Websocket的详解再也不介绍,个人上一篇文章已经介绍过了。html
pip install websocket
运行会报错,还须要依赖包websocket-clientweb
pip install websocket-client
使用一个demo测试网站:https://www.websocket.org/echo.html 进行演示。json
import json from websocket import create_connection url = 'wss://echo.websocket.org'#websocket链接地址,地址为虚拟地址 #websocket.enableTrace(True) #打开跟踪,查看日志 while True: ws = create_connection(url) #建立链接 data =input('输入传输消息:') #测试数据 # new_data=json.dumps(data,ensure_ascii=False) #将data转化为字符串 ws.send(data) #发送请求 print('收到回复消息:',ws.recv()) #打印服务器响应数据 if data == 'q': ws.close() #关闭链接 break