隨着html5的廣泛應用,基於websocket協議的即時通訊有了越來越多的使用場景,本文使用python中的websocket-client模塊來做相關的接口測試
1 import webclient 2 3 def creatWS(user,password):#生成ws鏈接的函數 4 token = getToken(user,password)#調用系統的登錄接口,獲取token 5 return "ws://url?token=%s"%token#生成ws鏈接 6 7 8 def msgChat(user,password):#即時聊天消息 9 websocket.enableTrace(True)#打開跟蹤,查看日志 10 strUrl= creatWS(user,password) 11 ws = websocket.create_connection(strUrl)#創建鏈接 12 data={json格式的數據} 13 ws.send(json.dumps(data)) 14 #print json.loads(ws.recv()) 15 print ws.recv()#查看返回 16 ws.close()