实现代码如下:
#udp协议通信
import socket,time
class UdpConnect:
def get_udp(self,ip,port,message):
#建立udp连接
mysocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
mysocket.connect((ip, port))
#发送数据包并关闭连接
mysocket.send(message.encode('GBK'))
mysocket.close()
if __name__ == '__main__':
ip='localhost'
port=554
# 拼接消息体,满足软件的协议规则(以下举例)
packetId = str(time.time())
name = "badbadboy"
host = 'MyHostName'
command = str(0x00000020)
content = 'hello world'
message = '1.0:' + packetId + ':' + name + ":" + host + ':' + command + ":" + content
UdpConnect().get_udp(ip,port,message)