websocket python实现原理

Posted sunshinekimi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了websocket python实现原理相关的知识,希望对你有一定的参考价值。

""" pip install ws4py """
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()

  

 

第二种:

运行效果也可以,run_forever里面有许多参数,需要自己设置

import websocket
 
 
def on_message(ws, message):  # 服务器有数据更新时,主动推送过来的数据
    print(message)
 
 
def on_error(ws, error):  # 程序报错时,就会触发on_error事件
    print(error)
 
 
def on_close(ws):
    print("Connection closed ……")
 
 
def on_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)

  

{"type":"btc_usdt.deep","ts":1556207364197,"data":{"asks":[5427.36,0.0368,5427.45,0.03,5427.56,0.046,5428,0.0046,5428.1,0.0364,5428.59,0.0018,5428.66,0.0013,5428.77,0.03,5428.96,0.011,5429.07,0.0009,5429.09,0.011,5429.35,0.0027,5429.36,0.011,5429.4399,0.0138,7065.5645,0.0028,7088.4157,0.0004,7102.5635,0.0006,7124.6346,0.0004],"bids":[5420.03,0.0212,5419.93,0.0013,5419.92,0.0276,5419.45,0.0026,5419.25,0.0184,5419.2,0.0044,5418.96,0.008,5418.81,0.0029,3637.2,0.2609,3637.1,0.8456,3636.7,0.277,3636.6,0.054,3636.5,0.513,3636.4,0.0933,3635.9,0.0247,3635.8,0.3,3635.3,0.022,3354.6,0.7547,3354.2,0.5,3353,0.018,3350.9,0.0126,3265.1,0.9432,3265,0.26,3248.9,0.125,3248.6,0.0147,3248.5,0.0248,3248.4,0.0106,3248.3,0.19,3248.2,0.0553,3248.1,0.1877,3247.9,0.4921,3247.8,0.757,3247.6,0.0119,3247.5,0.6,3247.4,0.453,3247.3,0.5924,3246.4,0.2148,3246,0.0485,3245.9,0.03,3245.8,0.0484,3245.1,0.2055,3245,0.1698,3244.3,0.16,3244.2,0.0523,3244,0.0071,3243.9,0.0239,3243.7,0.4803,3243.6,0.576,3243.5,0.576,3243.4,1.3858,3243,0.0369,3242.9,0.0049,3242.8,0.0816,3242.6,0.0739,3239.4,0.9298,3239.2,0.0134,3239.1,0.0234,3238.9,0.4802,3238.5,0.2239,3238.4,0.9849,3238.3,0.1379,3238.2,1,3238.1,0.0513,3238,0.0104,3237.8,0.7503,3237.7,0.9448,3237.6,0.1057,3237.5,0.0291,3237.4,0.15,3237.3,0.0543,3237.2,0.2068,3236.9,0.1115,3230.7,1.0153,3230.4,0.0264,3230.2,0.012,3230,0.0521,3229.9,0.6,3229.3,0.2004,3228.2,0.928,3228,0.0112,3227.9,0.5708,3227.6,0.5,3227.4,0.057,3227.2,0.0322,3227,0.2,3224.6,0.409,3224.5,0.2224,3224.4,0.14,3224.3,0.024,3224.2,0.0349,3224,0.0079,3223.8,0.0061,3223.6,0.8405,3223.4,0.927,3223.3,0.9606,3223.2,0.1011,3223.1,0.125,3223,0.0881,3222.9,0.9381,3222.8,0.6371]}}
{"type":"btc_usdt.deep","ts":1556207364207,"data":{"asks":[5427.36,0.0368,5427.45,0.03,5427.56,0.046,5428,0.0046,5428.1,0.0364,5428.59,0.0018,5428.66,0.0013,5428.77,0.03,5428.96,0.011,5429.07,0.0009,5429.35,0.0027,5429.36,0.011,5429.4399,0.0138,7065.5645,0.0028,7088.4157,0.0004,7102.5635,0.0006,7124.6346,0.0004],"bids":[5420.03,0.0212,5419.93,0.0013,5419.92,0.0276,5419.45,0.0026,5419.25,0.0184,5419.2,0.0044,5418.96,0.008,5418.81,0.0029,3637.2,0.2609,3637.1,0.8456,3636.7,0.277,3636.6,0.054,3636.5,0.513,3636.4,0.0933,3635.9,0.0247,3635.8,0.3,3635.3,0.022,3354.6,0.7547,3354.2,0.5,3353,0.018,3350.9,0.0126,3265.1,0.9432,3265,0.26,3248.9,0.125,3248.6,0.0147,3248.5,0.0248,3248.4,0.0106,3248.3,0.19,3248.2,0.0553,3248.1,0.1877,3247.9,0.4921,3247.8,0.757,3247.6,0.0119,3247.5,0.6,3247.4,0.453,3247.3,0.5924,3246.4,0.2148,3246,0.0485,3245.9,0.03,3245.8,0.0484,3245.1,0.2055,3245,0.1698,3244.3,0.16,3244.2,0.0523,3244,0.0071,3243.9,0.0239,3243.7,0.4803,3243.6,0.576,3243.5,0.576,3243.4,1.3858,3243,0.0369,3242.9,0.0049,3242.8,0.0816,3242.6,0.0739,3239.4,0.9298,3239.2,0.0134,3239.1,0.0234,3238.9,0.4802,3238.5,0.2239,3238.4,0.9849,3238.3,0.1379,3238.2,1,3238.1,0.0513,3238,0.0104,3237.8,0.7503,3237.7,0.9448,3237.6,0.1057,3237.5,0.0291,3237.4,0.15,3237.3,0.0543,3237.2,0.2068,3236.9,0.1115,3230.7,1.0153,3230.4,0.0264,3230.2,0.012,3230,0.0521,3229.9,0.6,3229.3,0.2004,3228.2,0.928,3228,0.0112,3227.9,0.5708,3227.6,0.5,3227.4,0.057,3227.2,0.0322,3227,0.2,3224.6,0.409,3224.5,0.2224,3224.4,0.14,3224.3,0.024,3224.2,0.0349,3224,0.0079,3223.8,0.0061,3223.6,0.8405,3223.4,0.927,3223.3,0.9606,3223.2,0.1011,3223.1,0.125,3223,0.0881,3222.9,0.9381,3222.8,0.6371]}}
{"type":"btc_usdt.deep","ts":1556207364220,"data":{"asks":[5427.36,0.0368,5427.45,0.03,5427.56,0.046,5428,0.0046,5428.1,0.0364,5428.59,0.0018,5428.66,0.0013,5428.77,0.03,5428.96,0.011,5429.07,0.0009,5429.35,0.0027,5429.36,0.011,7065.5645,0.0028,7088.4157,0.0004,7102.5635,0.0006,7124.6346,0.0004],"bids":[5420.03,0.0212,5419.93,0.0013,5419.92,0.0276,5419.45,0.0026,5419.25,0.0184,5419.2,0.0044,5418.96,0.008,5418.81,0.0029,3637.2,0.2609,3637.1,0.8456,3636.7,0.277,3636.6,0.054,3636.5,0.513,3636.4,0.0933,3635.9,0.0247,3635.8,0.3,3635.3,0.022,3354.6,0.7547,3354.2,0.5,3353,0.018,3350.9,0.0126,3265.1,0.9432,3265,0.26,3248.9,0.125,3248.6,0.0147,3248.5,0.0248,3248.4,0.0106,3248.3,0.19,3248.2,0.0553,3248.1,0.1877,3247.9,0.4921,3247.8,0.757,3247.6,0.0119,3247.5,0.6,3247.4,0.453,3247.3,0.5924,3246.4,0.2148,3246,0.0485,3245.9,0.03,3245.8,0.0484,3245.1,0.2055,3245,0.1698,3244.3,0.16,3244.2,0.0523,3244,0.0071,3243.9,0.0239,3243.7,0.4803,3243.6,0.576,3243.5,0.576,3243.4,1.3858,3243,0.0369,3242.9,0.0049,3242.8,0.0816,3242.6,0.0739,3239.4,0.9298,3239.2,0.0134,3239.1,0.0234,3238.9,0.4802,3238.5,0.2239,3238.4,0.9849,3238.3,0.1379,3238.2,1,3238.1,0.0513,3238,0.0104,3237.8,0.7503,3237.7,0.9448,3237.6,0.1057,3237.5,0.0291,3237.4,0.15,3237.3,0.0543,3237.2,0.2068,3236.9,0.1115,3230.7,1.0153,3230.4,0.0264,3230.2,0.012,3230,0.0521,3229.9,0.6,3229.3,0.2004,3228.2,0.928,3228,0.0112,3227.9,0.5708,3227.6,0.5,3227.4,0.057,3227.2,0.0322,3227,0.2,3224.6,0.409,3224.5,0.2224,3224.4,0.14,3224.3,0.024,3224.2,0.0349,3224,0.0079,3223.8,0.0061,3223.6,0.8405,3223.4,0.927,3223.3,0.9606,3223.2,0.1011,3223.1,0.125,3223,0.0881,3222.9,0.9381,3222.8,0.6371]}}
{"type":"btc_usdt.deep","ts":1556207364231,"data":{"asks":[5427.45,0.03,5427.56,0.046,5428,0.0046,5428.1,0.0364,5428.59,0.0018,5428.66,0.0013,5428.77,0.03,5428.96,0.011,5429.07,0.0009,5429.35,0.0027,5429.36,0.011,7065.5645,0.0028,7088.4157,0.0004,7102.5635,0.0006,7124.6346,0.0004],"bids":[5420.03,0.0212,5419.93,0.0013,5419.92,0.0276,5419.45,0.0026,5419.25,0.0184,5419.2,0.0044,5418.96,0.008,5418.81,0.0029,3637.2,0.2609,3637.1,0.8456,3636.7,0.277,3636.6,0.054,3636.5,0.513,3636.4,0.0933,3635.9,0.0247,3635.8,0.3,3635.3,0.022,3354.6,0.7547,3354.2,0.5,3353,0.018,3350.9,0.0126,3265.1,0.9432,3265,0.26,3248.9,0.125,3248.6,0.0147,3248.5,0.0248,3248.4,0.0106,3248.3,0.19,3248.2,0.0553,3248.1,0.1877,3247.9,0.4921,3247.8,0.757,3247.6,0.0119,3247.5,0.6,3247.4,0.453,3247.3,0.5924,3246.4,0.2148,3246,0.0485,3245.9,0.03,3245.8,0.0484,3245.1,0.2055,3245,0.1698,3244.3,0.16,3244.2,0.0523,3244,0.0071,3243.9,0.0239,3243.7,0.4803,3243.6,0.576,3243.5,0.576,3243.4,1.3858,3243,0.0369,3242.9,0.0049,3242.8,0.0816,3242.6,0.0739,3239.4,0.9298,3239.2,0.0134,3239.1,0.0234,3238.9,0.4802,3238.5,0.2239,3238.4,0.9849,3238.3,0.1379,3238.2,1,3238.1,0.0513,3238,0.0104,3237.8,0.7503,3237.7,0.9448,3237.6,0.1057,3237.5,0.0291,3237.4,0.15,3237.3,0.0543,3237.2,0.2068,3236.9,0.1115,3230.7,1.0153,3230.4,0.0264,3230.2,0.012,3230,0.0521,3229.9,0.6,3229.3,0.2004,3228.2,0.928,3228,0.0112,3227.9,0.5708,3227.6,0.5,3227.4,0.057,3227.2,0.0322,3227,0.2,3224.6,0.409,3224.5,0.2224,3224.4,0.14,3224.3,0.024,3224.2,0.0349,3224,0.0079,3223.8,0.0061,3223.6,0.8405,3223.4,0.927,3223.3,0.9606,3223.2,0.1011,3223.1,0.125,3223,0.0881,3222.9,0.9381,3222.8,0.6371]}}
{"type":"btc_usdt.deep","ts":1556207364246,"data":{"asks":[5427.45,0.03,5427.56,0.046,5428,0.0046,5428.59,0.0018,5428.66,0.0013,5428.77,0.03,5428.96,0.011,5429.07,0.0009,5429.35,0.0027,5429.36,0.011,7065.5645,0.0028,7088.4157,0.0004,7102.5635,0.0006,7124.6346,0.0004],"bids":[5420.03,0.0212,5419.93,0.0013,5419.92,0.0276,5419.45,0.0026,5419.25,0.0184,5419.2,0.0044,5418.96,0.008,5418.81,0.0029,3637.2,0.2609,3637.1,0.8456,3636.7,0.277,3636.6,0.054,3636.5,0.513,3636.4,0.0933,3635.9,0.0247,3635.8,0.3,3635.3,0.022,3354.6,0.7547,3354.2,0.5,3353,0.018,3350.9,0.0126,3265.1,0.9432,3265,0.26,3248.9,0.125,3248.6,0.0147,3248.5,0.0248,3248.4,0.0106,3248.3,0.19,3248.2,0.0553,3248.1,0.1877,3247.9,0.4921,3247.8,0.757,3247.6,0.0119,3247.5,0.6,3247.4,0.453,3247.3,0.5924,3246.4,0.2148,3246,0.0485,3245.9,0.03,3245.8,0.0484,3245.1,0.2055,3245,0.1698,3244.3,0.16,3244.2,0.0523,3244,0.0071,3243.9,0.0239,3243.7,0.4803,3243.6,0.576,3243.5,0.576,3243.4,1.3858,3243,0.0369,3242.9,0.0049,3242.8,0.0816,3242.6,0.0739,3239.4,0.9298,3239.2,0.0134,3239.1,0.0234,3238.9,0.4802,3238.5,0.2239,3238.4,0.9849,3238.3,0.1379,3238.2,1,3238.1,0.0513,3238,0.0104,3237.8,0.7503,3237.7,0.9448,3237.6,0.1057,3237.5,0.0291,3237.4,0.15,3237.3,0.0543,3237.2,0.2068,3236.9,0.1115,3230.7,1.0153,3230.4,0.0264,3230.2,0.012,3230,0.0521,3229.9,0.6,3229.3,0.2004,3228.2,0.928,3228,0.0112,3227.9,0.5708,3227.6,0.5,3227.4,0.057,3227.2,0.0322,3227,0.2,3224.6,0.409,3224.5,0.2224,3224.4,0.14,3224.3,0.024,3224.2,0.0349,3224,0.0079,3223.8,0.0061,3223.6,0.8405,3223.4,0.927,3223.3,0.9606,3223.2,0.1011,3223.1,0.125,3223,0.0881,3222.9,0.9381,3222.8,0.6371]}}
{"type":"btc_usdt.deep","ts":1556207364258,"data":{"asks":[5427.45,0.03,5427.56,0.046,5428,0.0046,5428.59,0.0018,5428.66,0.0013,5428.77,0.03,5428.96,0.011,5429.07,0.0009,5429.35,0.0027,7065.5645,0.0028,7088.4157,0.0004,7102.5635,0.0006,7124.6346,0.0004],"bids":[5420.27,0.0013,5420.03,0.0212,5419.93,0.0013,5419.92,0.0276,5419.45,0.0026,5419.25,0.0184,5419.2,0.0044,5418.96,0.008,5418.81,0.0029,3637.2,0.2609,3637.1,0.8456,3636.7,0.277,3636.6,0.054,3636.5,0.513,3636.4,0.0933,3635.9,0.0247,3635.8,0.3,3635.3,0.022,3354.6,0.7547,3354.2,0.5,3353,0.018,3350.9,0.0126,3265.1,0.9432,3265,0.26,3248.9,0.125,3248.6,0.0147,3248.5,0.0248,3248.4,0.0106,3248.3,0.19,3248.2,0.0553,3248.1,0.1877,3247.9,0.4921,3247.8,0.757,3247.6,0.0119,3247.5,0.6,3247.4,0.453,3247.3,0.5924,3246.4,0.2148,3246,0.0485,3245.9,0.03,3245.8,0.0484,3245.1,0.2055,3245,0.1698,3244.3,0.16,3244.2,0.0523,3244,0.0071,3243.9,0.0239,3243.7,0.4803,3243.6,0.576,3243.5,0.576,3243.4,1.3858,3243,0.0369,3242.9,0.0049,3242.8,0.0816,3242.6,0.0739,3239.4,0.9298,3239.2,0.0134,3239.1,0.0234,3238.9,0.4802,3238.5,0.2239,3238.4,0.9849,3238.3,0.1379,3238.2,1,3238.1,0.0513,3238,0.0104,3237.8,0.7503,3237.7,0.9448,3237.6,0.1057,3237.5,0.0291,3237.4,0.15,3237.3,0.0543,3237.2,0.2068,3236.9,0.1115,3230.7,1.0153,3230.4,0.0264,3230.2,0.012,3230,0.0521,3229.9,0.6,3229.3,0.2004,3228.2,0.928,3228,0.0112,3227.9,0.5708,3227.6,0.5,3227.4,0.057,3227.2,0.0322,3227,0.2,3224.6,0.409,3224.5,0.2224,3224.4,0.14,3224.3,0.024,3224.2,0.0349,3224,0.0079,3223.8,0.0061,3223.6,0.8405,3223.4,0.927,3223.3,0.9606,3223.2,0.1011,3223.1,0.125,3223,0.0881,3222.9,0.9381]}}
{"type":"btc_usdt.deep","ts":1556207364269,"data":{"asks":[5427.45,0.03,5428,0.0046,5428.59,0.0018,5428.66,0.0013,5428.77,0.03,5428.96,0.011,5429.07,0.0009,5429.35,0.0027,7065.5645,0.0028,7088.4157,0.0004,7102.5635,0.0006,7124.6346,0.0004],"bids":[5420.27,0.0013,5420.03,0.0212,5419.93,0.0013,5419.92,0.0276,5419.45,0.0026,5419.25,0.0184,5419.2,0.0044,5418.96,0.008,5418.81,0.0029,3637.2,0.2609,3637.1,0.8456,3636.7,0.277,3636.6,0.054,3636.5,0.513,3636.4,0.0933,3635.9,0.0247,3635.8,0.3,3635.3,0.022,3354.6,0.7547,3354.2,0.5,3353,0.018,3350.9,0.0126,3265.1,0.9432,3265,0.26,3248.9,0.125,3248.6,0.0147,3248.5,0.0248,3248.4,0.0106,3248.3,0.19,3248.2,0.0553,3248.1,0.1877,3247.9,0.4921,3247.8,0.757,3247.6,0.0119,3247.5,0.6,3247.4,0.453,3247.3,0.5924,3246.4,0.2148,3246,0.0485,3245.9,0.03,3245.8,0.0484,3245.1,0.2055,3245,0.1698,3244.3,0.16,3244.2,0.0523,3244,0.0071,3243.9,0.0239,3243.7,0.4803,3243.6,0.576,3243.5,0.576,3243.4,1.3858,3243,0.0369,3242.9,0.0049,3242.8,0.0816,3242.6,0.0739,3239.4,0.9298,3239.2,0.0134,3239.1,0.0234,3238.9,0.4802,3238.5,0.2239,3238.4,0.9849,3238.3,0.1379,3238.2,1,3238.1,0.0513,3238,0.0104,3237.8,0.7503,3237.7,0.9448,3237.6,0.1057,3237.5,0.0291,3237.4,0.15,3237.3,0.0543,3237.2,0.2068,3236.9,0.1115,3230.7,1.0153,3230.4,0.0264,3230.2,0.012,3230,0.0521,3229.9,0.6,3229.3,0.2004,3228.2,0.928,3228,0.0112,3227.9,0.5708,3227.6,0.5,3227.4,0.057,3227.2,0.0322,3227,0.2,3224.6,0.409,3224.5,0.2224,3224.4,0.14,3224.3,0.024,3224.2,0.0349,3224,0.0079,3223.8,0.0061,3223.6,0.8405,3223.4,0.927,3223.3,0.9606,3223.2,0.1011,3223.1,0.125,3223,0.0881,3222.9,0.9381]}}
{"type":"btc_usdt.deep","ts":1556207364279,"data":{"asks":[5427.45,0.03,5428,0.0046,5428.66,0.0013,5428.77,0.03,5428.96,0.011,5429.07,0.0009,5429.35,0.0027,7065.5645,0.0028,7088.4157,0.0004,7102.5635,0.0006,7124.6346,0.0004],"bids":[5420.27,0.0013,5420.03,0.0212,5419.93,0.0013,5419.92,0.0276,5419.45,0.0026,5419.25,0.0184,5419.2,0.0044,5418.96,0.008,5418.81,0.0029,3637.2,0.2609,3637.1,0.8456,3636.7,0.277,3636.6,0.054,3636.5,0.513,3636.4,0.0933,3635.9,0.0247,3635.8,0.3,3635.3,0.022,3354.6,0.7547,3354.2,0.5,3353,0.018,3350.9,0.0126,3265.1,0.9432,3265,0.26,3248.9,0.125,3248.6,0.0147,3248.5,0.0248,3248.4,0.0106,3248.3,0.19,3248.2,0.0553,3248.1,0.1877,3247.9,0.4921,3247.8,0.757,3247.6,0.0119,3247.5,0.6,3247.4,0.453,3247.3,0.5924,3246.4,0.2148,3246,0.0485,3245.9,0.03,3245.8,0.0484,3245.1,0.2055,3245,0.1698,3244.3,0.16,3244.2,0.0523,3244,0.0071,3243.9,0.0239,3243.7,0.4803,3243.6,0.576,3243.5,0.576,3243.4,1.3858,3243,0.0369,3242.9,0.0049,3242.8,0.0816,3242.6,0.0739,3239.4,0.9298,3239.2,0.0134,3239.1,0.0234,3238.9,0.4802,3238.5,0.2239,3238.4,0.9849,3238.3,0.1379,3238.2,1,3238.1,0.0513,3238,0.0104,3237.8,0.7503,3237.7,0.9448,3237.6,0.1057,3237.5,0.0291,3237.4,0.15,3237.3,0.0543,3237.2,0.2068,3236.9,0.1115,3230.7,1.0153,3230.4,0.0264,3230.2,0.012,3230,0.0521,3229.9,0.6,3229.3,0.2004,3228.2,0.928,3228,0.0112,3227.9,0.5708,3227.6,0.5,3227.4,0.057,3227.2,0.0322,3227,0.2,3224.6,0.409,3224.5,0.2224,3224.4,0.14,3224.3,0.024,3224.2,0.0349,3224,0.0079,3223.8,0.0061,3223.6,0.8405,3223.4,0.927,3223.3,0.9606,3223.2,0.1011,3223.1,0.125,3223,0.0881,3222.9,0.9381]}}
{"type":"btc_usdt.deep","ts":1556207364290,"data":{"asks":[5428,0.0046,5428.66,0.0013,5428.77,0.03,5428.96,0.011,5429.07,0.0009,5429.35,0.0027,7065.5645,0.0028,7088.4157,0.0004,7102.5635,0.0006,7124.6346,0.0004],"bids":[5420.27,0.0013,5420.03,0.0212,5419.93,0.0013,5419.92,0.0276,5419.45,0.0026,5419.25,0.0184,5419.2,0.0044,5418.96,0.008,5418.81,0.0029,3637.2,0.2609,3637.1,0.8456,3636.7,0.277,3636.6,0.054,3636.5,0.513,3636.4,0.0933,3635.9,0.0247,3635.8,0.3,3635.3,0.022,3354.6,0.7547,3354.2,0.5,3353,0.018,3350.9,0.0126,3265.1,0.9432,3265,0.26,3248.9,0.125,3248.6,0.0147,3248.5,0.0248,3248.4,0.0106,3248.3,0.19,3248.2,0.0553,3248.1,0.1877,3247.9,0.4921,3247.8,0.757,3247.6,0.0119,3247.5,0.6,3247.4,0.453,3247.3,0.5924,3246.4,0.2148,3246,0.0485,3245.9,0.03,3245.8,0.0484,3245.1,0.2055,3245,0.1698,3244.3,0.16,3244.2,0.0523,3244,0.0071,3243.9,0.0239,3243.7,0.4803,3243.6,0.576,3243.5,0.576,3243.4,1.3858,3243,0.0369,3242.9,0.0049,3242.8,0.0816,3242.6,0.0739,3239.4,0.9298,3239.2,0.0134,3239.1,0.0234,3238.9,0.4802,3238.5,0.2239,3238.4,0.9849,3238.3,0.1379,3238.2,1,3238.1,0.0513,3238,0.0104,3237.8,0.7503,3237.7,0.9448,3237.6,0.1057,3237.5,0.0291,3237.4,0.15,3237.3,0.0543,3237.2,0.2068,3236.9,0.1115,3230.7,1.0153,3230.4,0.0264,3230.2,0.012,3230,0.0521,3229.9,0.6,3229.3,0.2004,3228.2,0.928,3228,0.0112,3227.9,0.5708,3227.6,0.5,3227.4,0.057,3227.2,0.0322,3227,0.2,3224.6,0.409,3224.5,0.2224,3224.4,0.14,3224.3,0.024,3224.2,0.0349,3224,0.0079,3223.8,0.0061,3223.6,0.8405,3223.4,0.927,3223.3,0.9606,3223.2,0.1011,3223.1,0.125,3223,0.0881,3222.9,0.9381]}}
{"type":"btc_usdt.deep","ts":1556207364299,"data":{"asks":[5428,0.0046,5428.66,0.0013,5428.77,0.03,5429.07,0.0009,5429.27,0.0385,5429.35,0.0027,7065.5645,0.0028,7088.4157,0.0004,7102.5635,0.0006,7124.6346,0.0004],"bids":[5420.27,0.0013,5420.03,0.0212,5419.93,0.0013,5419.92,0.0276,5419.45,0.0026,5419.25,0.0184,5419.2,0.0044,5418.96,0.008,5418.81,0.0029,3637.2,0.2609,3637.1,0.8456,3636.7,0.277,3636.6,0.054,3636.5,0.513,3636.4,0.0933,3635.9,0.0247,3635.8,0.3,3635.3,0.022,3354.6,0.7547,3354.2,0.5,3353,0.018,3350.9,0.0126,3265.1,0.9432,3265,0.26,3248.9,0.125,3248.6,0.0147,3248.5,0.0248,3248.4,0.0106,3248.3,0.19,3248.2,0.0553,3248.1,0.1877,3247.9,0.4921,3247.8,0.757,3247.6,0.0119,3247.5,0.6,3247.4,0.453,3247.3,0.5924,3246.4,0.2148,3246,0.0485,3245.9,0.03,3245.8,0.0484,3245.1,0.2055,3245,0.1698,3244.3,0.16,3244.2,0.0523,3244,0.0071,3243.9,0.0239,3243.7,0.4803,3243.6,0.576,3243.5,0.576,3243.4,1.3858,3243,0.0369,3242.9,0.0049,3242.8,0.0816,3242.6,0.0739,3239.4,0.9298,3239.2,0.0134,3239.1,0.0234,3238.9,0.4802,3238.5,0.2239,3238.4,0.9849,3238.3,0.1379,3238.2,1,3238.1,0.0513,3238,0.0104,3237.8,0.7503,3237.7,0.9448,3237.6,0.1057,3237.5,0.0291,3237.4,0.15,3237.3,0.0543,3237.2,0.2068,3236.9,0.1115,3230.7,1.0153,3230.4,0.0264,3230.2,0.012,3230,0.0521,3229.9,0.6,3229.3,0.2004,3228.2,0.928,3228,0.0112,3227.9,0.5708,3227.6,0.5,3227.4,0.057,3227.2,0.0322,3227,0.2,3224.6,0.409,3224.5,0.2224,3224.4,0.14,3224.3,0.024,3224.2,0.0349,3224,0.0079,3223.8,0.0061,3223.6,0.8405,3223.4,0.927,3223.3,0.9606,3223.2,0.1011,3223.1,0.125,3223,0.0881,3222.9,0.9381]}}
{"type":"btc_usdt.deep","ts":1556207364309,"data":{"asks":[5428,0.0046,5428.66,0.0013,5428.77,0.03,5429.07,0.0009,5429.27,0.0385,7065.5645,0.0028,7088.4157,0.0004,7102.5635,0.0006,7124.6346,0.0004],"bids":[5420.27,0.0013,5420.03,0.0212,5419.93,0.0013,5419.92,0.0276,5419.45,0.0026,5419.25,0.0184,5419.2,0.0044,5418.96,0.008,5418.81,0.0029,3637.2,0.2609,3637.1,0.8456,3636.7,0.277,3636.6,0.054,3636.5,0.513,3636.4,0.0933,3635.9,0.0247,3635.8,0.3,3635.3,0.022,3354.6,0.7547,3354.2,0.5,3353,0.018,3350.9,0.0126,3265.1,0.9432,3265,0.26,3248.9,0.125,3248.6,0.0147,3248.5,0.0248,3248.4,0.0106,3248.3,0.19,3248.2,0.0553,3248.1,0.1877,3247.9,0.4921,3247.8,0.757,3247.6,0.0119,3247.5,0.6,3247.4,0.453,3247.3,0.5924,3246.4,0.2148,3246,0.0485,3245.9,0.03,3245.8,0.0484,3245.1,0.2055,3245,0.1698,3244.3,0.16,3244.2,0.0523,3244,0.0071,3243.9,0.0239,3243.7,0.4803,3243.6,0.576,3243.5,0.576,3243.4,1.3858,3243,0.0369,3242.9,0.0049,3242.8,0.0816,3242.6,0.0739,3239.4,0.9298,3239.2,0.0134,3239.1,0.0234,3238.9,0.4802,3238.5,0.2239,3238.4,0.9849,3238.3,0.1379,3238.2,1,3238.1,0.0513,3238,0.0104,3237.8,0.7503,3237.7,0.9448,3237.6,0.1057,3237.5,0.0291,3237.4,0.15,3237.3,0.0543,3237.2,0.2068,3236.9,0.1115,3230.7,1.0153,3230.4,0.0264,3230.2,0.012,3230,0.0521,3229.9,0.6,3229.3,0.2004,3228.2,0.928,3228,0.0112,3227.9,0.5708,3227.6,0.5,3227.4,0.057,3227.2,0.0322,3227,0.2,3224.6,0.409,3224.5,0.2224,3224.4,0.14,3224.3,0.024,3224.2,0.0349,3224,0.0079,3223.8,0.0061,3223.6,0.8405,3223.4,0.927,3223.3,0.9606,3223.2,0.1011,3223.1,0.125,3223,0.0881,3222.9,0.9381]}}
{"type":"btc_usdt.deep","ts":1556207364312,"data":{"asks":[5425.06,0.0084,5428,0.0046,5428.38,0.0212,5428.66,0.0013,5428.77,0.03,5429.07,0.0009,5429.27,0.0385,7065.5645,0.0028,7088.4157,0.0004,7102.5635,0.0006,7124.6346,0.0004],"bids":[5420.27,0.0013,5420.24,0.0276,5420.03,0.0212,5419.93,0.0013,5419.92,0.0276,5419.77,0.0027,5419.45,0.0026,5419.25,0.0184,5419.2,0.0044,5418.96,0.008,5418.81,0.0029,3637.2,0.2609,3637.1,0.8456,3636.7,0.277,3636.6,0.054,3636.5,0.513,3636.4,0.0933,3635.9,0.0247,3635.8,0.3,3635.3,0.022,3354.6,0.7547,3354.2,0.5,3353,0.018,3350.9,0.0126,3265.1,0.9432,3265,0.26,3248.9,0.125,3248.6,0.0147,3248.5,0.0248,3248.4,0.0106,3248.3,0.19,3248.2,0.0553,3248.1,0.1877,3247.9,0.4921,3247.8,0.757,3247.6,0.0119,3247.5,0.6,3247.4,0.453,3247.3,0.5924,3246.4,0.2148,3246,0.0485,3245.9,0.03,3245.8,0.0484,3245.1,0.2055,3245,0.1698,3244.3,0.16,3244.2,0.0523,3244,0.0071,3243.9,0.0239,3243.7,0.4803,3243.6,0.576,3243.5,0.576,3243.4,1.3858,3243,0.0369,3242.9,0.0049,3242.8,0.0816,3242.6,0.0739,3239.4,0.9298,3239.2,0.0134,3239.1,0.0234,3238.9,0.4802,3238.5,0.2239,3238.4,0.9849,3238.3,0.1379,3238.2,1,3238.1,0.0513,3238,0.0104,3237.8,0.7503,3237.7,0.9448,3237.6,0.1057,3237.5,0.0291,3237.4,0.15,3237.3,0.0543,3237.2,0.2068,3236.9,0.1115,3230.7,1.0153,3230.4,0.0264,3230.2,0.012,3230,0.0521,3229.9,0.6,3229.3,0.2004,3228.2,0.928,3228,0.0112,3227.9,0.5708,3227.6,0.5,3227.4,0.057,3227.2,0.0322,3227,0.2,3224.6,0.409,3224.5,0.2224,3224.4,0.14,3224.3,0.024,3224.2,0.0349,3224,0.0079,3223.8,0.0061,3223.6,0.8405,3223.4,0.927,3223.3,0.9606,3223.2,0.1011,3223.1,0.125]}}
{"type":"btc_usdt.deep","ts":1556207364315,"data":{"asks":[5425.06,0.0084,5428,0.0046,5428.38,0.0212,5428.66,0.0013,5428.77,0.03,5429.07,0.0009,5429.27,0.0385,5430,1.665,7065.5645,0.0028,7088.4157,0.0004,7102.5635,0.0006,7124.6346,0.0004],"bids":[5420.27,0.0013,5420.24,0.0276,5420.03,0.0212,5419.93,0.0013,5419.92,0.0276,5419.77,0.0027,5419.45,0.0026,5419.25,0.0184,5419.24,0.0368,5419.2,0.0044,5418.96,0.008,5418.81,0.0029,3637.2,0.2609,3637.1,0.8456,3636.7,0.277,3636.6,0.054,3636.5,0.513,3636.4,0.0933,3635.9,0.0247,3635.8,0.3,3635.3,0.022,3354.6,0.7547,3354.2,0.5,3353,0.018,3350.9,0.0126,3265.1,0.9432,3265,0.26,3248.9,0.125,3248.6,0.0147,3248.5,0.0248,3248.4,0.0106,3248.3,0.19,3248.2,0.0553,3248.1,0.1877,3247.9,0.4921,3247.8,0.757,3247.6,0.0119,3247.5,0.6,3247.4,0.453,3247.3,0.5924,3246.4,0.2148,3246,0.0485,3245.9,0.03,3245.8,0.0484,3245.1,0.2055,3245,0.1698,3244.3,0.16,3244.2,0.0523,3244,0.0071,3243.9,0.0239,3243.7,0.4803,3243.6,0.576,3243.5,0.576,3243.4,1.3858,3243,0.0369,3242.9,0.0049,3242.8,0.0816,3242.6,0.0739,3239.4,0.9298,3239.2,0.0134,3239.1,0.0234,3238.9,0.4802,3238.5,0.2239,3238.4,0.9849,3238.3,0.1379,3238.2,1,3238.1,0.0513,3238,0.0104,3237.8,0.7503,3237.7,0.9448,3237.6,0.1057,3237.5,0.0291,3237.4,0.15,3237.3,0.0543,3237.2,0.2068,3236.9,0.1115,3230.7,1.0153,3230.4,0.0264,3230.2,0.012,3230,0.0521,3229.9,0.6,3229.3,0.2004,3228.2,0.928,3228,0.0112,3227.9,0.5708,3227.6,0.5,3227.4,0.057,3227.2,0.0322,3227,0.2,3224.6,0.409,3224.5,0.2224,3224.4,0.14,3224.3,0.024,3224.2,0.0349,3224,0.0079,3223.8,0.0061,3223.6,0.8405,3223.4,0.927,3223.3,0.9606,3223.2,0.1011]}}
{"type":"btc_usdt.deep","ts":1556207364403,"data":{"asks":[5428,0.0046,5428.38,0.0212,5428.66,0.0013,5428.72,0.0027,5428.77,0.03,5429.07,0.0009,5429.27,0.0385,5429.37,0.0147,5429.95,0.2099,5430,1.665,5430.06,0.0423,5430.1,0.1785,5430.52,0.03,5430.56,0.0147,7065.5645,0.0028,7088.4157,0.0004,7102.5635,0.0006,7124.6346,0.0004],"bids":[5420.27,0.0013,5420.24,0.0276,5420.03,0.0212,5419.93,0.0013,5419.92,0.0276,5419.77,0.0027,5419.45,0.0026,5419.25,0.0184,5419.24,0.0368,5419.2,0.0044,5418.96,0.008,5418.81,0.0029,3637.2,0.2609,3637.1,0.8456,3636.7,0.277,3636.6,0.054,3636.5,0.513,3636.4,0.0933,3635.9,0.0247,3635.8,0.3,3635.3,0.022,3354.6,0.7547,3354.2,0.5,3353,0.018,3350.9,0.0126,3265.1,0.9432,3265,0.26,3248.9,0.125,3248.6,0.0147,3248.5,0.0248,3248.4,0.0106,3248.3,0.19,3248.2,0.0553,3248.1,0.1877,3247.9,0.4921,3247.8,0.757,3247.6,0.0119,3247.5,0.6,3247.4,0.453,3247.3,0.5924,3246.4,0.2148,3246,0.0485,3245.9,0.03,3245.8,0.0484,3245.1,0.2055,3245,0.1698,3244.3,0.16,3244.2,0.0523,3244,0.0071,3243.9,0.0239,3243.7,0.4803,3243.6,0.576,3243.5,0.576,3243.4,1.3858,3243,0.0369,3242.9,0.0049,3242.8,0.0816,3242.6,0.0739,3239.4,0.9298,3239.2,0.0134,3239.1,0.0234,3238.9,0.4802,3238.5,0.2239,3238.4,0.9849,3238.3,0.1379,3238.2,1,3238.1,0.0513,3238,0.0104,3237.8,0.7503,3237.7,0.9448,3237.6,0.1057,3237.5,0.0291,3237.4,0.15,3237.3,0.0543,3237.2,0.2068,3236.9,0.1115,3230.7,1.0153,3230.4,0.0264,3230.2,0.012,3230,0.0521,3229.9,0.6,3229.3,0.2004,3228.2,0.928,3228,0.0112,3227.9,0.5708,3227.6,0.5,3227.4,0.057,3227.2,0.0322,3227,0.2,3224.6,0.409,3224.5,0.2224,3224.4,0.14,3224.3,0.024,3224.2,0.0349,3224,0.0079,3223.8,0.0061,3223.6,0.8405,3223.4,0.927,3223.3,0.9606,3223.2,0.1011]}}

以上是关于websocket python实现原理的主要内容,如果未能解决你的问题,请参考以下文章

python---websocket的使用

python 实现websocket

前端页面实现报警器提示音效果

Pywss - 用python实现WebSocket服务端

WebSocket简介及实现原理

WebSocket的实现原理