python测试基于websocket协议的即时通讯接口

时间:2021-05-11 06:47:49

随着html5的广泛应用,基于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()