码迷,mamicode.com
首页 > 编程语言 > 详细

python3 websocket客户端

时间:2021-06-07 21:05:52      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:send   style   直接   cstring   ever   message   ack   client   time   

python客户端代码,可直接使用

import websocket
import json
import time
import threading

class WebsocketClient(object):
    """docstring for WebsocketClient"""
    def __init__(self, address, message_callback=None):
        super(WebsocketClient, self).__init__()
        self.address = address
        self.message_callback = message_callback

    def on_message(self, ws, message):
        # message = json.loads(message)
        print("on_client_message:", message)
        if self.message_callback:
            self.message_callback(message)

    def on_error(self, ws, error):
        print("client error:",error)

    def on_close(self, ws):
        print("### client closed ###")
        self.ws.close()
        self.is_running = False

    def on_open(self, ws):
        self.is_running = True
        print("on open")

    def close_connect(self):
        self.ws.close()
    
    def send_message(self, message):
        try:
            self.ws.send(message)
        except BaseException as err:
            pass
        

    def run(self):
        websocket.enableTrace(True)
        self.ws = websocket.WebSocketApp(self.address,
                              on_message = lambda ws,message: self.on_message(ws, message),
                              on_error = lambda ws, error: self.on_error(ws, error),
                              on_close = lambda ws :self.on_close(ws))
        self.ws.on_open = lambda ws: self.on_open(ws)
        self.is_running = False
        while True:
            print(self.is_running)
            if not self.is_running:
                self.ws.run_forever()
            time.sleep(3)


class WSClient(object):
    def __init__(self, address, call_back):
        super(WSClient, self).__init__()
        self.client = WebsocketClient(address, call_back)
        self.client_thread = None

    def run(self):
        self.client_thread = threading.Thread(target=self.run_client)
        self.client_thread.start()

    def run_client(self):
        self.client.run()

    def send_message(self, message):
        self.client.send_message(message)

ws_client = WSClient("ws://localhost:8888", lambda message : print("call_back message:",message))
ws_client.run()

while True:
    ws_client.send_message("time:" + str(time.time()))
    time.sleep(5)

 

python3 websocket客户端

标签:send   style   直接   cstring   ever   message   ack   client   time   

原文地址:https://www.cnblogs.com/ushowtime/p/14859212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!