跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

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

发表于
最近拿 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.')

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.