python实现的WebSocket客户端

python

code

#coding=utf-8

import json

import time

from websocket import create_connection

ws = create_connection("ws://x.x.x.x:8090/haiyou/device")

print("Sending 'Hello, World'...")

t=str(time.time()).split(".")[0]

params={

"version": 1,

"msgNo": "1",

"machNo": "U040119110001",

"cmd": 1,

"time": 1574064604418

}

ws.send(json.dumps(params))

print("Sent")

print("Reeiving...")

result = ws.recv()

print("Received '{}'".format(result))

params={

"version": 1,

"msgNo": t,

"machNo": "U040119110001",

"cmd": 7,

"time": t,

"data": {

"userId": 8,

"companyType": 3,

"before": 0,

"after": 100,

"openTime": t,

"closeTime": t

}

}

ws.send(json.dumps(params))

print("Sent")

print("Reeiving...")

result = ws.recv()

print("Received '{}'".format(result))

ws.close()

 整理后

#coding=utf-8

import json

import time

from websocket import create_connection

class 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()*1000).split(".")[0]

address="ws://39.106.85.158:8090/haiyou/device"

params1={

"version": 1,

"msgNo":t,

"machNo": "U040119110001",

"cmd": 1,

"time":t

}

params2={

"version": 1,

"msgNo": t,

"machNo": "U040119110001",

"cmd": 7,

"time": t,

"data": {

"userId": 8,

"companyType": 3,

"before": 0,

"after": 100,

"openTime": t,

"closeTime": t

}

}

#初始化

webso=websocket(address)

#发送数据

webso.send(params1)

webso.send(params2)

#断开连接

webso.quit()

参考:

https://blog.csdn.net/qq562029186/article/details/81203893

以上是 python实现的WebSocket客户端 的全部内容, 来源链接: utcz.com/z/387041.html

回到顶部