Python测试基于websocket协议的即时通讯接口【多测师】
使用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() #关闭连接
相关推荐
congcongsuiyue 2020-03-15
NameCHC 2019-12-16
shufen0 2019-12-06
lemontreeshy 2019-11-16
csdnuuu 2019-11-12
goawalk 2015-05-08
fayeyiwang 2019-11-06
hoganch 2019-11-03
happyfling 2019-09-20
luckysyy 2019-08-21
kongge 2019-08-05
hufeng00 2019-08-21
ningningmingming 2015-05-08
fengyun0 2016-07-04
kaifaer 2013-10-08
j0lin 2012-09-19