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

全屏阅读
  • 基本信息
  • 作者:
  • 作者已发布:924篇文章
  • 发布时间:2023年09月08日 17:48:21
  • 所属分类:python3
  • 阅读次数:249次阅读
  • 标签:
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,就直接注释掉

顶一下
(0)
100%
订阅 回复
踩一下
(0)
100%
» 郑重声明:本文由mpxq168发布,所有内容仅代表个人观点。版权归恒富网mpxq168共有,欢迎转载, 但未经作者同意必须保留此段声明,并给出文章连接,否则保留追究法律责任的权利! 如果本文侵犯了您的权益,请留言。

目前有 0 条留言 其中:访客:0 条, 博主:0 条

给我留言

您必须 [ 登录 ] 才能发表留言!