WebSocket-for-Python
WebSocket-for-Python copied to clipboard
断线重连怎么做?
from ws4py.client.threadedclient import WebSocketClient from ws4py.exc import HandshakeError class DummyClient(WebSocketClient): def opened(self): self.send(input('请输入你想说的话:'))
def closed(self, code, reason=None):
#print("Closed down", code, reason)
print("Closed down")
def received_message(self, m):
print (m)
if name == 'main': try: ws = DummyClient('ws://'+input('请输入ws地址连接:'), protocols=['chat']) ws.connect() ws.run_forever() except KeyboardInterrupt: ws.close()
同求,我在配合爬虫使用的时候,运行几个小时后,会报1006going away的错误,然后关闭了
count = 0
while True:
try:
ws = WSClient('xxx')
print("Start contenting...")
ws.connect()
ws.run_forever()
except KeyboardInterrupt:
ws.close()
break
except Exception as e:
count += 1
print(e, "Try again %s times..." % count)
continue
I hope it helps you