当前位置:   article > 正文

python websocket 连接_python连接websocket

python连接websocket
class WebSocket(object):
    def __init__(self, url, req):
        self.url = url
        self.req = req
        self.ws = ''

    def on_open(self):  # 连接到服务器之后就会触发on_open事件,这里用于send数据
        logger.info(self.req)
        self.ws.send(self.req)
        return self.ws

    def web_socket_connect(self):
        websocket.enableTrace(True)
        self.ws = websocket.WebSocketApp(self.url)
        logger.info('websocket connected')
        # self.ws.on_open = self.on_open
        # self.ws.run_forever(ping_timeout=5)
        sleep(0.5)
        self.ws.close()
        logger.info('websocket closed')


if __name__ == "__main__":
    req = '{"event":"subscribe", "channel":"btc_usdt.deep"}'
    wst = WebSocket("wss://i.cg.net/wi/ws", req)
    wst.web_socket_connect()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/85469
推荐阅读
相关标签
  

闽ICP备14008679号