Python websockets库测试websocket协议

一、websockets库安装

pip install websockets

 

二、代码

# -*- coding: utf-8 -*-
import websocket           
import json

url = 'wss://xxxxxx.net'#websocket连接地址,地址为虚拟地址
#websocket.enableTrace(True)                      #打开跟踪,查看日志
ws = websocket.create_connection(url)            #创建连接
data = {"api":"map",
        "cmd":"set",
        "request_message_id":"abcdef123456",
        "args":{"south_lat":-90,"north_lat":90,"west_lng":-180,"east_lng":180}}         #测试数据

new_data=json.dumps(data,ensure_ascii=False)     #将data转化为字符串
ws.send(new_data)                                #发送请求
print(ws.recv())                                 #打印服务器响应数据
ws.close()                                       #关闭连接
posted @ 2020-03-10 15:12  疯狂的机器人  阅读(3526)  评论(0编辑  收藏  举报