尝试在 pythonanywhere 上使用 praw 提交图像时出现 WebSocketProxyException 403

WebSocketProxyException 403 when trying to submit image with praw on pythonanywhere

我正在尝试在 pythonanywhere 上使用 praw 将图像本地上传到 reddit。

praw.models.Subreddit.submit_image(title, image_path)

这在我的电脑上运行良好,但在 pythonanywhere 上它抛出 403 错误。 (虽然图片仍然会上传。)

我看到 on pythonanywhere's website 403 错误是由于他们的代理白名单,但 reddit 是白名单网站之一,而 praw 使用 requests,pythonanywhere 说这是一个兼容的库。常规 submit 也可以正常工作。

File "/home/ibid/.local/lib/python3.7/site-packages/praw/models/reddit/subreddit.py", line 780, in submit_image
    return self._submit_media(data, timeout)
File "/home/ibid/.local/lib/python3.7/site-packages/praw/models/reddit/subreddit.py", line 494, in _submit_media
    response["json"]["data"]["websocket_url"], timeout=timeout
File "/home/ibid/.local/lib/python3.7/site-packages/websocket/_core.py", line 514, in create_connection
    websock.connect(url, **options)
File "/home/ibid/.local/lib/python3.7/site-packages/websocket/_core.py", line 223, in connect
    options.pop('socket', None))
File "/home/ibid/.local/lib/python3.7/site-packages/websocket/_http.py", line 122, in connect
    sock = _tunnel(sock, hostname, port, auth)
File "/home/ibid/.local/lib/python3.7/site-packages/websocket/_http.py", line 293, in _tunnel
    "failed CONNECT via proxy status: %r" % status)
websocket._exceptions.WebSocketProxyException: failed CONNECT via proxy status: 403

我在 PRAW 中编写了 image-submit 功能。正如 the documentation 中所述,Reddit API 在通过 API 提交图像或视频时使用 websockets。这与 API 对文本或 link 提交的工作方式不同,后者直接 return 创建的 post 的 URL。当涉及到图像和视频时,响应类似于

{'json': {'data': {'user_submitted_page': 'https://www.reddit.com/user/username/submitted/',
                   'websocket_url': 'wss://ws-05ba9e4989f78959d.wss.redditmedia.com/rte_images/z4a98g21vsb31?m=AQAAHFs1XR-NRjAq9D4PPYA6SuDvFIMUGGv2Vpv5ePM2bLV6wf5o'},
          'errors': []}}

然后我们必须监听 websocket URL 以最终获得创建的 post 的 url。如果你有兴趣,相关代码是 here.


如您所见,websocket URLs(至少在回答这个问题时)往往是 *.wss.redditmedia.com 的子域。 redditmedia.com 不在 the PythonAnywhere whitelist 上(尽管 reddit.com 本身在),因此连接失败。

我看到了一些适合你的解决方案,这里先给出我认为最理想的:

  1. Giles Thomas(发表评论的人)或来自 PythonAnywhere 的其他人将 wss.redditmedia.com.
  2. 的子域列入白名单
  3. 在PRAW >=6.5.0中,传递参数without_websockets=True禁用websockets。如 the documentation of submit_image 中所述,将其设置为 True 意味着您根本不会获得 return 值。
  4. 在 PRAW <6.5.0 中,在每次使用 submit_image 时使用 try-except 块。正如您所发现的,posts 仍将被创建。但是,您不会收到 Submission 对象作为 return 值,因为 websocket 将失败。
  5. 付费升级到 non-whitelisted 版本的 PythonAnywhere。