详解python websocket获取实时数据的几种常见链接方式
第一种,使用create_connection链接,需要pipinstallwebsocket-client(此方法不建议使用,链接不稳定,容易断,并且连接很耗时)
importtime fromwebsocketimportcreate_connection url='wss://i.cg.net/wi/ws' whileTrue:#一直链接,直到连接上就退出循环 time.sleep(2) try: ws=create_connection(url) print(ws) break exceptExceptionase: print('连接异常:',e) continue whileTrue:#连接上,退出第一个循环之后,此循环用于一直获取数据 ws.send('{"event":"subscribe","channel":"btc_usdt.ticker"}') response=ws.recv() print(response)
第二种,运行效果很不错,很容易连接,获取数据的速度也挺快
importjson fromws4py.client.threadedclientimportWebSocketClient classCG_Client(WebSocketClient): defopened(self): req='{"event":"subscribe","channel":"eth_usdt.deep"}' self.send(req) defclosed(self,code,reason=None): print("Closeddown:",code,reason) defreceived_message(self,resp): resp=json.loads(str(resp)) data=resp['data'] iftype(data)isdict: ask=data['asks'][0] print('Ask:',ask) bid=data['bids'][0] print('Bid:',bid) if__name__=='__main__': ws=None try: ws=CG_Client('wss://i.cg.net/wi/ws') ws.connect() ws.run_forever() exceptKeyboardInterrupt: ws.close()
第三种,其实和第一种差不多,只不过换种写法而已,运行效果不理想,连接耗时,并且容易断
importwebsocket whileTrue: ws=websocket.WebSocket() try: ws.connect("wss://i.cg.net/wi/ws") print(ws) break exceptExceptionase: print('异常:',e) continue print('OK') whileTrue: req='{"event":"subscribe","channel":"btc_usdt.deep"}' ws.send(req) resp=ws.recv() print(resp)
第四种,运行效果也可以,run_forever里面有许多参数,需要自己设置
importwebsocket defon_message(ws,message):#服务器有数据更新时,主动推送过来的数据 print(message) defon_error(ws,error):#程序报错时,就会触发on_error事件 print(error) defon_close(ws): print("Connectionclosed……") defon_open(ws):#连接到服务器之后就会触发on_open事件,这里用于send数据 req='{"event":"subscribe","channel":"btc_usdt.deep"}' print(req) ws.send(req) if__name__=="__main__": websocket.enableTrace(True) ws=websocket.WebSocketApp("wss://i.cg.net/wi/ws", on_message=on_message, on_error=on_error, on_close=on_close) ws.on_open=on_open ws.run_forever(ping_timeout=30)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。