最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解python websocket獲取實(shí)時(shí)數(shù)據(jù)的幾種常見鏈接方式

 更新時(shí)間:2019年07月01日 09:24:16   作者:Jerry_JD  
這篇文章主要介紹了詳解python websocket獲取實(shí)時(shí)數(shù)據(jù)的幾種常見鏈接方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

第一種, 使用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í)和第一種差不多,只不過換種寫法而已,運(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)推送過來的數(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)

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

赤水市| 安新县| 青海省| 正镶白旗| 清原| 贵德县| 宜宾市| 义乌市| 西华县| 邳州市| 全州县| 察雅县| 五常市| 科尔| 梅河口市| 祁连县| 台中市| 保定市| 遂川县| 渝中区| 汶上县| 浦江县| 宁武县| 合阳县| 宾阳县| 保靖县| 遂川县| 巫山县| 仙居县| 海口市| 陵川县| 芜湖市| 兴海县| 柏乡县| 太和县| 青阳县| 舟曲县| 凌海市| 承德县| 恭城| 宣武区|