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

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.

  • Yes

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

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 27 (10 by maintainers)

Most upvoted comments

Yep, working on a fix for that right now!

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.

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.

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