python监控(bian)网站websocket示例代码

import gzip
import json

import os
os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"

import websocket
import base64
import random
def on_message(ws, message):
    # print(bytes(message))
    data = gzip.decompress(message).decode("utf-8")  # gip解压
    
    dict1 = json.loads(data)
    print(dict1 )


def on_error(ws, error):
    print(error)
def on_close(ws, close_status_code, close_msg):
    print("### closed ###")
def on_open(ws):
    print("Opened connection")
    ws.send('{"method":"SUBSCRIBE","params":["btcusdt@aggSnap","!contractInfo","!miniTicker@arr","btcusdt@markPrice","!markPriceMini@arr","btcusdt@depth@500ms","btcusdt_perpetual@continuousKline_1d"],"id":1}')
if __name__ == "__main__":
    while True:
        try:
            # websocket.enableTrace(True)
            ws = websocket.WebSocketApp('wss://sfstream.binance.com/stream',

                                      keep_running=True,
                                      on_open=on_open,
                                      on_message=on_message,
                                      on_error=on_error,
                                      on_close=on_close)

            ws.run_forever()  # Set dispatcher to automatic reconnection
        except:
            pass

需要fq

os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"

改成你自己的代理就行,或者默认可以fq,就直接注释掉

猜你喜欢

转载自blog.csdn.net/m0_38124502/article/details/124493315
今日推荐