V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tgfdier
V2EX  ›  Python

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

  •  
  •   tgfdier · 51 天前 · 1140 次点击
    这是一个创建于 51 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近拿 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.')
    6 条回复    2024-07-19 12:47:49 +08:00
    julyclyde
        1
    julyclyde  
       50 天前
    你为啥认为是 ssl 阶段呢?
    tgfdier
        2
    tgfdier  
    OP
       50 天前
    @julyclyde

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

    所以你需要换一个库,比如这个:
    https://websocket-client.readthedocs.io/en/latest/examples.html#connecting-through-a-proxy
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1163 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:50 · PVG 07:50 · LAX 16:50 · JFK 19:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.