詳解python websocket獲取實(shí)時(shí)數(shù)據(jù)的幾種常見(jiàn)鏈接方式
第一種, 使用create_connection鏈接,需要pip install websocket-client (此方法不建議使用,鏈接不穩(wěn)定,容易斷,并且連接很耗時(shí))
import time from websocket import create_connection url = 'wss://i.cg.net/wi/ws' while True: # 一直鏈接,直到連接上就退出循環(huán) time.sleep(2) try: ws = create_connection(url) print(ws) break except Exception as e: print('連接異常:', e) continue while True: # 連接上,退出第一個(gè)循環(huán)之后,此循環(huán)用于一直獲取數(shù)據(jù) ws.send('{"event":"subscribe", "channel":"btc_usdt.ticker"}') response = ws.recv() print(response)
第二種,運(yùn)行效果很不錯(cuò),很容易連接,獲取數(shù)據(jù)的速度也挺快
import json from ws4py.client.threadedclient import WebSocketClient class CG_Client(WebSocketClient): def opened(self): req = '{"event":"subscribe", "channel":"eth_usdt.deep"}' self.send(req) def closed(self, code, reason=None): print("Closed down:", code, reason) def received_message(self, resp): resp = json.loads(str(resp)) data = resp['data'] if type(data) is dict: 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() except KeyboardInterrupt: ws.close()
第三種,其實(shí)和第一種差不多,只不過(guò)換種寫法而已,運(yùn)行效果不理想,連接耗時(shí),并且容易斷
import websocket while True: ws = websocket.WebSocket() try: ws.connect("wss://i.cg.net/wi/ws") print(ws) break except Exception as e: print('異常:', e) continue print('OK') while True: req = '{"event":"subscribe", "channel":"btc_usdt.deep"}' ws.send(req) resp = ws.recv() print(resp)
第四種,運(yùn)行效果也可以,run_forever里面有許多參數(shù),需要自己設(shè)置
import websocket def on_message(ws, message): # 服務(wù)器有數(shù)據(jù)更新時(shí),主動(dòng)推送過(guò)來(lái)的數(shù)據(jù) print(message) def on_error(ws, error): # 程序報(bào)錯(cuò)時(shí),就會(huì)觸發(fā)on_error事件 print(error) def on_close(ws): print("Connection closed ……") def on_open(ws): # 連接到服務(wù)器之后就會(huì)觸發(fā)on_open事件,這里用于send數(shù)據(jù) 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)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python Websocket服務(wù)端通信的使用示例
- Python通過(guò)websocket與js客戶端通信示例分析
- 使用python構(gòu)建WebSocket客戶端的教程詳解
- python實(shí)現(xiàn)WebSocket服務(wù)端過(guò)程解析
- python實(shí)現(xiàn)websocket的客戶端壓力測(cè)試
- Python如何爬取實(shí)時(shí)變化的WebSocket數(shù)據(jù)的方法
- python制作websocket服務(wù)器實(shí)例分享
- Python 實(shí)現(xiàn) WebSocket 通信的過(guò)程詳解
相關(guān)文章
Python報(bào)錯(cuò):NameError:?name?‘xxx‘?is?not?defined的解決辦法
這篇文章主要給大家介紹了關(guān)于Python報(bào)錯(cuò):NameError:?name?‘xxx‘?is?not?defined的解決辦法,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-06-06Python3實(shí)現(xiàn)zip分卷壓縮過(guò)程解析
這篇文章主要介紹了Python3實(shí)現(xiàn)zip分卷壓縮過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Python連接PostgreSQL數(shù)據(jù)庫(kù)的方法
大家應(yīng)該都有所了解,python可以操作多種數(shù)據(jù)庫(kù),諸如SQLite、MySql、PostgreSQL等,這里不對(duì)所有的數(shù)據(jù)庫(kù)操作方法進(jìn)行贅述,只針對(duì)目前項(xiàng)目中用到的PostgreSQL做一下簡(jiǎn)單介紹,主要是Python連接PostgreSQL數(shù)據(jù)庫(kù)的方法。有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-11-11Python PyTorch 如何獲取 MNIST 數(shù)據(jù)
這篇文章主要介紹了Python PyTorch 如何獲取 MNIST 數(shù)據(jù),通過(guò)示例代碼介紹了PyTorch 保存 MNIST 數(shù)據(jù),PyTorch 顯示 MNIST 數(shù)據(jù)的操作方法,感興趣的朋友跟隨小編一起看看吧2024-04-04Django處理Ajax發(fā)送的Get請(qǐng)求代碼詳解
在本篇文章里小編給大家整理了關(guān)于Django處理Ajax發(fā)送的Get請(qǐng)求代碼知識(shí)點(diǎn),有需要的朋友們參考學(xué)習(xí)下。2019-07-07