我正在使用websockets模块,但它不支持通过我的公司代理连接客户端连接:
我的最佳选择是什么?还有其他模块可以用来建立客户端WebSocket连接吗?
>>> import asyncio, websockets
>>> async def connect(uri):
... async with websockets.connect(uri) as websocket:
... pass
...
>>> asyncio.get_event_loop().run_until_complete(connect('ws://myhost.com/path/'))
....
ConnectionRefusedError: [Errno 10061] Connect call failed ('myhost.com', 80)
然而,如果使用设置了http_proxy
环境变量的curl,则会起作用:
$ curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: MTIzNDEyMzQxMjM0MTIzNA==" http://myhost.com/path/
HTTP/1.1 101 Switching Protocols
Server: nginx/1.13.6
Date: Fri, 10 Nov 2017 14:51:00 GMT
Upgrade: websocket
Sec-WebSocket-Accept: s+CT5bkW5F3N2/5JUXrCPtLHn68=
Connection: Upgrade
我的最佳选择是什么?还有其他模块可以用来建立客户端WebSocket连接吗?
websockets
不支持通过代理连接客户端。https://websockets.readthedocs.io/en/stable/limitations.html因此,请使用 https://pypi.python.org/pypi/websocket-client。 - Ahsanwebsockets
并转而使用websocket-client
的地方。实际上,我曾经在这两个之间做出选择。所以代理对于websockets
是有限制的。 - Ahsan