python 测试ws接口
最近项目有个长连应用测试,所以用python简单demo一下,废话不多说,直接上代码
import websocket #需要安装ws包 ,pip install websocket
from urllib import parse
ws_url = "ws://127.0.0.1/ws"
ws_token = "*********"
ws_para = '{"jid":“”****“”}'
ws_ts = '12463327854358'
ws_reqpara = "?para="+parse.quote(ws_para )+"&token="+parse.quote(ws_token )+"&ts="+ws_ts #应用规定接入websocket所需要的认证信息
def on_message(ws,message):
print("接收message:")
print(message)
def on_error(ws,error):
print("异常信息error:")
print(error)
def on_close(ws):
print("连接关闭...")
if __name__=="__main__":
ws = websocket.WebSocketApp(ws_url+ws_reqpara,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()
#以上代码为建立连接,实施监听状态和接收消息