woniyu 2020-06-02
使用python中的websocket-client模块来做相关的接口测试 import webclient def creatWS(user,password):#生成ws链接的函数 token = getToken(user,password)#调用系统的登录接口,获取token return "ws://url?token=%s"%token#生成ws链接 def msgChat(user,password):#即时聊天消息 websocket.enableTrace(True)#打开跟踪,查看日志 strUrl= creatWS(user,password) ws = websocket.create_connection(strUrl)#创建链接 data={json格式的数据} ws.send(json.dumps(data)) #print json.loads(ws.recv()) print ws.recv()#查看返回 ws.close()
import websocket url = ‘wss://host:port/rt/tr‘ #websocket连接地址 ws = websocket.create_connection(url) #创建连接‘‘‘data为json格式‘‘‘ data = {"body":{"address":"陕西省","location":{"latitude":11.23256618923611,"longitude":218.9637478298611,"timestamp":1541735071192},"state":"arrivalDepot","timestamp":1541862074000,"trackId":3218111000038001,"transportId":218111000038},"token":"token","type":"roadtrace_state"} ws.send(json.dumps(data)) #json转化为字符串,必须转化 print(ws.recv()) #服务器响应数据 ws.close() #关闭连接