栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python连接websocket

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python连接websocket

import _thread
import json
import ssl
import time
import websocket

def on_message(ws, message):
    print(ws)
    print(message)


def on_error(ws, error):
    print(ws)
    print(error)


def on_close(ws):
    print(ws)
    print("closed")


def on_open(ws):
    def run(*args):
        # 建立连接后自动订阅
        ws.send(json.dumps({}))
        while True:
            time.sleep(10)
            ws.send(json.dumps({"ping": int(time.time() * 1000)}))
    _thread.start_new_thread(run, ())


if __name__ == "__main__":
    url = ""
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp(url,
                                on_open=on_open,
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close)
    # wss跳过SSL验证
    ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/339269.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号