[新手求助] (可能是)websocket 的代理问题

60 天前
 tgfdier
最近拿 python 写了一个访问实时币价的小脚本,在单位电脑上正常运行,但是在家里的电脑上会访问超时,似乎是代理的问题(试过访问墙内 ws 是没问题的),但是没有印象单位和家里电脑代理设置有什么不同,特来求助大佬

用的 python 的 aiowebsocket 库,翻了下代码是在 ssl 握手的时候超时造成的,但是文档没看到代理相关设置,clash 里使用的混合端口设置也未发现可能的问题

错误信息:
Traceback (most recent call last):
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\websockets\legacy\client.py", line 647, in __await_impl_timeout__
return await self.__await_impl__()
^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\websockets\legacy\client.py", line 651, in __await_impl__
_transport, _protocol = await self._create_connection()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 1069, in create_connection
sock = await self._connect_sock(
^^^^^^^^^^^^^^^^^^^^^^^^^

File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 973, in _connect_sock
await self.sock_connect(sock, address)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\asyncio\proactor_events.py", line 726, in sock_connect
return await self._proactor.connect(sock, address)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\xxx\Desktop\1\p.py", line 15, in <module>
asyncio.get_event_loop().run_until_complete(clientRun())
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^

File "C:\Users\xxx\Desktop\1\p.py", line 5, in clientRun
async with websockets.connect('wss://stream.binance.com/stream') as websocket:
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\websockets\legacy\client.py", line 629, in __aenter__
return await self
^^^^^^^^^^
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\websockets\legacy\client.py", line 646, in __await_impl_timeout__
async with asyncio_timeout(self.open_timeout):
File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\asyncio\timeouts.py", line 111, in __aexit__
raise TimeoutError from exc_val
TimeoutError

代码:
import asyncio
import logging
from aiowebsocket.converses import AioWebSocket
import json

async def startup(uri):
async with AioWebSocket(uri) as aws:
converse = aws.manipulator
await converse.send('{"method":"SUBSCRIBE","params":["!miniTicker@arr@3000ms"],"id":1}')
while True:
mes = await converse.receive()
# print(json.loads(mes.decode()))

if __name__ == '__main__':
remote = 'wss://stream.binance.com/stream'
try:
asyncio.get_event_loop().run_until_complete(startup(remote))
except KeyboardInterrupt as exc:
logging.info('Quit.')
1161 次点击
所在节点    Python
6 条回复
julyclyde
59 天前
你为啥认为是 ssl 阶段呢?
tgfdier
59 天前
@julyclyde

看报错信息是 create_connection()→open_connection(host=host, port=port, ssl=ssl) 这个地方超时的,我是小白,最近才接触这个,难道不是这个问题吗
julyclyde
59 天前
@tgfdier callstack 里都没有 ssl 相关函数啊
看着像 tcp 连接建立的时候就已经超时了吧
julyclyde
59 天前
我建议的检查思路:
1 在家的网络上,是概率失败,还是稳定失败?
2 使用这个脚本以外的其它工具先试试能不能连上
fkdtz
59 天前
第一个思路是验证是否走上了代理。可以看看代理的日志。
还可以对比一下公司电脑和家里电脑上代理的配置有什么不同,例如是不是 TUN 模式之类。
另一个思路是就在代码中手动配置使用代理。
bhy
59 天前
这种库大概率不支持代理的。就算支持的你也要自己写在代码里,一般不会自动用代理。

所以你需要换一个库,比如这个:
https://websocket-client.readthedocs.io/en/latest/examples.html#connecting-through-a-proxy

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1058386

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX