1. 程式人生 > >python指令碼測試websocket介面協議

python指令碼測試websocket介面協議

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() #關閉連線

在執行程式碼前,先安裝websocket模組: