我是websockets的新手,我正在尝试在python上运行以下代码
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import cgi
import json
print('Content-type: text/html\r\n\r;charset=utf-8')
import websockets
import asyncio
result = "global"
async def hello3():
async with websockets.connect("wss://********") as websocket:
json_string = """{"m":0,"i":220,"n":"GetTickerHistory","o":[{"InstrumentId":1,"FromDate":1551506400}]}"""
data = json.dumps(json_string)
await websocket.send(data)
global result
result = await websocket.recv()
asyncio.get_event_loop().run_until_complete(hello3())
print(result)
问题是,当我尝试在终端中运行此代码时,程序被卡住,然后当我单击ctrl + c停止它时,出现以下错误
^CTraceback (most recent call last):
File "BitvoTicker.cgi", line 22, in <module>
asyncio.get_event_loop().run_until_complete(hello3())
File "/usr/lib/python3.6/asyncio/base_events.py", line 460, in run_until_complete
self.run_forever()
File "/usr/lib/python3.6/asyncio/base_events.py", line 427, in run_forever
self._run_once()
File "/usr/lib/python3.6/asyncio/base_events.py", line 1404, in _run_once
event_list = self._selector.select(timeout)
File "/usr/lib/python3.6/selectors.py", line 445, in select
fd_event_list = self._epoll.poll(timeout, max_ev)
KeyboardInterrupt
当我注释掉websocket.recv时,程序将运行