使用Python v3.5或v3.6當連接到某些websocket服務器時,websocket客戶端不關閉。下面的示例代碼顯示了當連接到wss://echo.websocket.org時該進程正常工作,但在連接到wss:/stream.pushbullet.com時未能斷開連接。aiohttp Websocket在連接到某些websocket服務器時不關閉
任何人都可以看到有什麼區別?幾乎看起來它應該與服務器有關,以及它如何表現(或者可能是行爲不當)。
import asyncio
import aiohttp
# Code: http://pastebin.com/G5sfpQG2
# Closing the echo.websocket.org connection works as expected
# Closing the stream.pushbullet.com connection hangs
async def run():
session = aiohttp.ClientSession()
API_KEY = "RrFnc1xaeQXnRrr2auoGA1e8pQ8MWmMF" # (OK to have here)
async with session.ws_connect('wss://stream.pushbullet.com/websocket/' + API_KEY) as ws:
# async with session.ws_connect("wss://echo.websocket.org") as ws:
ws.send_json({"hello": "world"})
async def _timeout():
await asyncio.sleep(2)
print('closing ... ', end="", flush=True)
await ws.close()
print('... closed. Should see "broke out of ..." messages next')
asyncio.get_event_loop().create_task(_timeout())
async for ws_msg in ws:
print("ws_msg:", ws_msg)
print("broke out of async for loop")
print("broke out of async with")
session.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
print("goodbye")
這不提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/16737768) – kdopen