praw-dev / praw

PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.
http://praw.readthedocs.io/
BSD 2-Clause "Simplified" License
3.4k stars 453 forks source link

Praw submit_image/submit_video with websockets failing as of 3:06 pm Central on Sep. 29, 2023 #1980

Closed grantmcconnaughey closed 9 months ago

grantmcconnaughey commented 9 months ago

Describe the Bug

Starting about 33 minutes ago the Praw submit_image and submit_video methods began failing with the error message ConnectionRefusedError: [Errno 111] Connection refused when submitting image and video posts using websockets. without_websockets=True does seem to work, but notably does not return a submission object, which means callers do not receive the newly created submission or submission ID.

I'm not sure if this is an intentional change by Reddit or not, as this is an undocumented API and they likely won't announce changes to it either way.

Desired Result

Image and video posts should submit with websockets.

Code to reproduce the bug

reddit.subreddit(subreddit).submit_image(title='My title', timeout=30, image_path='/tmp/image.png')

The Reddit() initialization in my code example does not include the following parameters to prevent credential leakage:

client_secret, password, or refresh_token.

Relevant Logs

Traceback (most recent call last):
  File "/var/task/praw/models/reddit/subreddit.py", line 613, in _submit_media
    connection = websocket.create_connection(websocket_url, timeout=timeout)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/task/websocket/_core.py", line 610, in create_connection
    websock.connect(url, **options)
  File "/var/task/websocket/_core.py", line 251, in connect
    self.sock, addrs = connect(url, self.sock_opt, proxy_info(**options),
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/task/websocket/_http.py", line 129, in connect
    sock = _open_socket(addrinfo_list, options.sockopt, options.timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/task/websocket/_http.py", line 204, in _open_socket
    raise err
  File "/var/task/websocket/_http.py", line 184, in _open_socket
    sock.connect(address)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/var/task/apps/reddit/bot.py", line 285, in submit_post
    reddit_submission = subreddit.submit_image(**submission_kwargs)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/task/praw/util/deprecate_args.py", line 43, in wrapped
    return func(**dict(zip(_old_args, args)), **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/task/praw/models/reddit/subreddit.py", line 1263, in submit_image
    return self._submit_media(
           ^^^^^^^^^^^^^^^^^^^
  File "/var/task/praw/models/reddit/subreddit.py", line 619, in _submit_media
    raise WebSocketException(
praw.exceptions.WebSocketException: Error establishing websocket connection.

This code has previously worked as intended.

Yes

Operating System/Environment

Linux

Python Version

3.11

PRAW Version

7.6.0

Prawcore Version

2.3.0

Anything else?

No response

LilSpazJoekp commented 9 months ago

They might be blocking image uploads. It could also be a service outage. We'll have to wait and see.

gorkemye commented 9 months ago

I have same issue.

cimspin commented 9 months ago

Still ongoing as of 13:54 UTC.

LilSpazJoekp commented 9 months ago

Could you try updating to this version and see if it resolves the issue?

pip install --upgrade https://github.com/praw-dev/praw/archive/LilSpazJoekp-patch-1.zip
gorkemye commented 9 months ago

Could you try updating to this version and see if it resolves the issue?

pip install --upgrade https://github.com/praw-dev/praw/archive/LilSpazJoekp-patch-1.zip

I got this error after update. Error while posting image to subreddit: received 403 HTTP response

LilSpazJoekp commented 9 months ago

Hmm I have to do some more checking. Did you check if a post was actually made? Also did it print a stack trace?

gorkemye commented 9 months ago

No post has not been created, It just returned " received 403 HTTP response"

seaweeduk commented 9 months ago

Doesn't resolve the issue for me either see below

self.request( File "/home/user/.local/lib/python3.10/site-packages/praw/util/deprecate_args.py", line 43, in wrapped return func(**dict(zip(_old_args, args)), **kwargs) File "/home/user/.local/lib/python3.10/site-packages/praw/reddit.py", line 941, in request return self._core.request( File "/home/user/.local/lib/python3.10/site-packages/prawcore/sessions.py", line 330, in request return self._request_with_retries( File "/home/user/.local/lib/python3.10/site-packages/prawcore/sessions.py", line 266, in _request_with_retries raise self.STATUS_EXCEPTIONS[response.status_code](response) prawcore.exceptions.Forbidden: received 403 HTTP response

grantmcconnaughey commented 9 months ago

This Reddit post seems to think this is happening because the websocket URL has moved: https://www.reddit.com/r/redditdev/comments/16wfw86/apisubmit_upload_websocket_fix/

LilSpazJoekp commented 9 months ago

Yep, working on a fix for that right now!

grantmcconnaughey commented 9 months ago

Awesome, thank you so much!

gorkemye commented 9 months ago

Yep, working on a fix for that right now!

Thank you !

LilSpazJoekp commented 9 months ago

Got a fix ready. Can someone test to confirm? It worked for me when writing tests but want to make sure it works for y'all.

pip install --upgrade https://github.com/praw-dev/praw/archive/fix-image-uploads.zip
gorkemye commented 9 months ago

Got a fix ready. Can someone test to confirm? It worked for me when writing tests but want to make sure it works for y'all.

pip install --upgrade https://github.com/praw-dev/praw/archive/fix-image-uploads.zip

I tried to test it but get 403 error again. Full Traceback:


Traceback (most recent call last):                                                                                                       
  File "x\reddit_bot.py", line 32, in submit_to_subreddit                    
    submission = subreddit.submit_image(title=title, image_path=image_path)                                                              
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                              
  File "x\env\Lib\site-packages\praw\util\deprecate_args.py", line 45, in wrapped           
    return func(**dict(zip(_old_args, args)), **kwargs)                                                                                  
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                  
  File "x\env\Lib\site-packages\praw\models\reddit\subreddit.py", line 1508, in submit_image
    image_url, websocket_url = self._upload_media(                                                                                       
                               ^^^^^^^^^^^^^^^^^^^                                                                                       
  File "x\env\Lib\site-packages\praw\models\reddit\subreddit.py", line 965, in _upload_media
    upload_response = self._reddit.post(url, data=img_data)                                                                              
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                              
  File "x\env\Lib\site-packages\praw\util\deprecate_args.py", line 45, in wrapped           
    return func(**dict(zip(_old_args, args)), **kwargs)                                                                                  
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "x\env\Lib\site-packages\praw\reddit.py", line 838, in post
    return self._objectify_request(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "x\env\Lib\site-packages\praw\reddit.py", line 513, in _objectify_request
    self.request(
  File "x\env\Lib\site-packages\praw\util\deprecate_args.py", line 45, in wrapped
    return func(**dict(zip(_old_args, args)), **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "x\env\Lib\site-packages\praw\reddit.py", line 939, in request
    return self._core.request(
           ^^^^^^^^^^^^^^^^^^^
  File "x\env\Lib\site-packages\prawcore\sessions.py", line 330, in request
    return self._request_with_retries(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "xt\env\Lib\site-packages\prawcore\sessions.py", line 266, in _request_with_retries
    raise self.STATUS_EXCEPTIONS[response.status_code](response)
prawcore.exceptions.Forbidden: received 403 HTTP response
LilSpazJoekp commented 9 months ago

Got a fix ready. Can someone test to confirm? It worked for me when writing tests but want to make sure it works for y'all.

pip install --upgrade https://github.com/praw-dev/praw/archive/fix-image-uploads.zip

I tried to test it but get 403 error again. Full Traceback:

Traceback (most recent call last):                                                                                                       
  File "C:\Users\MSI\PycharmProjects\django-amazon-bot\products\tools\reddit_bot.py", line 32, in submit_to_subreddit                    
    submission = subreddit.submit_image(title=title, image_path=image_path)                                                              
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                              
  File "x\env\Lib\site-packages\praw\util\deprecate_args.py", line 45, in wrapped           
    return func(**dict(zip(_old_args, args)), **kwargs)                                                                                  
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                  
  File "x\env\Lib\site-packages\praw\models\reddit\subreddit.py", line 1508, in submit_image
    image_url, websocket_url = self._upload_media(                                                                                       
                               ^^^^^^^^^^^^^^^^^^^                                                                                       
  File "x\env\Lib\site-packages\praw\models\reddit\subreddit.py", line 965, in _upload_media
    upload_response = self._reddit.post(url, data=img_data)                                                                              
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                              
  File "x\env\Lib\site-packages\praw\util\deprecate_args.py", line 45, in wrapped           
    return func(**dict(zip(_old_args, args)), **kwargs)                                                                                  
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "x\env\Lib\site-packages\praw\reddit.py", line 838, in post
    return self._objectify_request(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "x\env\Lib\site-packages\praw\reddit.py", line 513, in _objectify_request
    self.request(
  File "x\env\Lib\site-packages\praw\util\deprecate_args.py", line 45, in wrapped
    return func(**dict(zip(_old_args, args)), **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "x\django-amazon-bot\env\Lib\site-packages\praw\reddit.py", line 939, in request
    return self._core.request(
           ^^^^^^^^^^^^^^^^^^^
  File "x\env\Lib\site-packages\prawcore\sessions.py", line 330, in request
    return self._request_with_retries(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "xt\env\Lib\site-packages\prawcore\sessions.py", line 266, in _request_with_retries
    raise self.STATUS_EXCEPTIONS[response.status_code](response)
prawcore.exceptions.Forbidden: received 403 HTTP response

This stacktrace does not match the code in the branch. More specifically image_url, websocket_url = self._upload_media( doesn't exist anymore.

gorkemye commented 9 months ago

Got a fix ready. Can someone test to confirm? It worked for me when writing tests but want to make sure it works for y'all.

pip install --upgrade https://github.com/praw-dev/praw/archive/fix-image-uploads.zip

I tried to test it but get 403 error again. Full Traceback:

Traceback (most recent call last):                                                                                                       
  File "C:\Users\MSI\PycharmProjects\django-amazon-bot\products\tools\reddit_bot.py", line 32, in submit_to_subreddit                    
    submission = subreddit.submit_image(title=title, image_path=image_path)                                                              
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                              
  File "x\env\Lib\site-packages\praw\util\deprecate_args.py", line 45, in wrapped           
    return func(**dict(zip(_old_args, args)), **kwargs)                                                                                  
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                  
  File "x\env\Lib\site-packages\praw\models\reddit\subreddit.py", line 1508, in submit_image
    image_url, websocket_url = self._upload_media(                                                                                       
                               ^^^^^^^^^^^^^^^^^^^                                                                                       
  File "x\env\Lib\site-packages\praw\models\reddit\subreddit.py", line 965, in _upload_media
    upload_response = self._reddit.post(url, data=img_data)                                                                              
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                              
  File "x\env\Lib\site-packages\praw\util\deprecate_args.py", line 45, in wrapped           
    return func(**dict(zip(_old_args, args)), **kwargs)                                                                                  
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "x\env\Lib\site-packages\praw\reddit.py", line 838, in post
    return self._objectify_request(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "x\env\Lib\site-packages\praw\reddit.py", line 513, in _objectify_request
    self.request(
  File "x\env\Lib\site-packages\praw\util\deprecate_args.py", line 45, in wrapped
    return func(**dict(zip(_old_args, args)), **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "x\django-amazon-bot\env\Lib\site-packages\praw\reddit.py", line 939, in request
    return self._core.request(
           ^^^^^^^^^^^^^^^^^^^
  File "x\env\Lib\site-packages\prawcore\sessions.py", line 330, in request
    return self._request_with_retries(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "xt\env\Lib\site-packages\prawcore\sessions.py", line 266, in _request_with_retries
    raise self.STATUS_EXCEPTIONS[response.status_code](response)
prawcore.exceptions.Forbidden: received 403 HTTP response

This stacktrace does not match the code in the branch. More specifically image_url, websocket_url = self._upload_media( doesn't exist anymore.

I did a clean install from scratch. It worked perfectly ! PS: On my first run, I got the error "websocket error occurred, it may still have been created, please check", but on my second run, it succeeded without entering the except block.

aPeoplesCalendar commented 9 months ago

Thank you for addressing this so quickly. I can confirm the linked package solved my issue as well!

oklopfer commented 9 months ago

Likewise can confirm, branch fix-image-uploads indeed fixes image uploads!

gorkemye commented 9 months ago

@LilSpazJoekp I'm just letting you know for informational purposes, sometimes it gives a websocket error consecutively. It loads the image, but when I try to reply to the related submission, it returns a NoneType.

LilSpazJoekp commented 9 months ago

@LilSpazJoekp I'm just letting you know for informational purposes, sometimes it gives a websocket error consecutively. It loads the image, but when I try to reply to the related submission, it returns a NoneType.

What does it look like on Reddit?

gorkemye commented 9 months ago

@LilSpazJoekp I'm just letting you know for informational purposes, sometimes it gives a websocket error consecutively. It loads the image, but when I try to reply to the related submission, it returns a NoneType.

What does it look like on Reddit?

It creates the post with the image, but it can't reply because submission returns a NoneType.

     submission = subreddit.submit_image(title=title, image_path=image_path)
        if submission:
            self.reply_to_submission(submission, text)

*This happens when websocket gives error.

LilSpazJoekp commented 9 months ago

Gotcha. Then I'll need to do some more changes to it.

oklopfer commented 9 months ago

just to clarify, you are saying that PRAW can't send replies backend - but on the website frontend, one would still be able to, correct?

Because I have been able to do the latter

gorkemye commented 9 months ago

@LilSpazJoekp Hello, any update with this situation ?

LilSpazJoekp commented 9 months ago

Currently looking but so far it seems that if the websocket fails or you tell PRAW to not use websockets, PRAW won't be able to return a submission object because there isn't anything in the responses that returns the new submission ID.

grantmcconnaughey commented 9 months ago

Currently looking but so far it seems that if the websocket fails or you tell PRAW to not use websockets, PRAW won't be able to return a submission object because there isn't anything in the responses that returns the new submission ID.

As far as I know that's how it has always worked before.

For what it's worth I've deployed your fix for the last week or two and it's been working great. Or at least working the same as before Reddit broke the URL.

There is still an issue where nothing is returned if you submit without websockets. This is documented. Plus if websockets fail the yes, naturally there isn't anything we can do in PRAW other than retry, I guess.

LilSpazJoekp commented 9 months ago

Gotcha. Then there isn't much PRAW can do if it doesn't to connect to the web socket. I'll get the fix merged in soon.