class WebSocket(object): def __init__(self, url, req): self.url = url self.req = req self.ws = '' def on_open(self): # Triggered when connected to the server on_open event , Here for send data 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()