凌的博客

您现在的位置是:首页  > 学无止境 > python

python

python websocket服务端、客户端

2020-05-04python0

server端:

# pip installer pywss
from pywss import Pyws, route
import time


@route('/log')
def log(request, data):
    info = "{0}:{1}".format(time.strftime("%Y-%m-%d %M:%H:%S"), data)
    print(data)
    return info

if __name__ == '__main__':
    try:
        ip = "127.0.0.1"
        port = 8866
        ws = Pyws(__name__, address=ip, port=port)
        ws.serve_forever()
    except Exception as e:
        print("Error:%s" % str(e))

客户端:

# pip install websocket-client
from websocket import create_connection

ws = create_connection("ws://localhost:8866/log")
msg = "Hello, World"
print("发送消息:%s" % msg)
ws.send(msg)
# print("发送中...")
result = ws.recv()
print("返回结果:%s" % result)
ws.close()


如果本文对您有所帮助,请支持下本站哦!!!^_^

文章评论

0条评论