
正文
python实现的WebSocket客户端
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
code
#coding=utf-
import json
import time
from websocket import create_connectionws = create_connection("ws://x.x.x.x:8090/haiyou/device")
print("Sending 'Hello, World'...")
t=str(time.time()).split(".")[]
params={
"version": ,
"msgNo": "",
"machNo": "U040119110001",
"cmd": ,
"time":
}ws.send(json.dumps(params))
print("Sent")
print("Reeiving...")
result = ws.recv()
print("Received '{}'".format(result))params={
"version": ,
"msgNo": t,
"machNo": "U040119110001",
"cmd": ,
"time": t,
"data": {
"userId": ,
"companyType": ,
"before": ,
"after": ,
"openTime": t,
"closeTime": t
}
}ws.send(json.dumps(params))
print("Sent")
print("Reeiving...")
result = ws.recv()
print("Received '{}'".format(result))ws.close()
整理后
#coding=utf-
import json
import time
from websocket import create_connectionclass websocket:
def __init__(self,address):
self.ws = create_connection(address) def send(self,params):
print("Sending ...")
self.ws.send(json.dumps(params))
print("Reeiving...")
result = self.ws.recv()
print("Received '{}'".format(result)) def quit(self):
self.ws.close()t=str(time.time()*).split(".")[]
address="ws://39.106.85.158:8090/haiyou/device"params1={
"version": ,
"msgNo":t,
"machNo": "U040119110001",
"cmd": ,
"time":t
}params2={
"version": ,
"msgNo": t,
"machNo": "U040119110001",
"cmd": ,
"time": t,
"data": {
"userId": ,
"companyType": ,
"before": ,
"after": ,
"openTime": t,
"closeTime": t
}
}#初始化
webso=websocket(address)#发送数据
webso.send(params1)
webso.send(params2)#断开连接
webso.quit()
参考:
https://blog.csdn.net/qq562029186/article/details/81203893





