Python 格式 UDP 消息格式十六进制
Python format UDP messages format hexadecimal
你好朋友我在 python 有一个基于 UDP 协议的套接字服务器,当接收到 gps 设备发送的消息时,teltonika 向我显示以下内容:
'\ x00b \ xca \ xfe \ x01 \ x16 \ x00 \ x0f359632103146970 \ x08 \ x01 \ x00 \ x00 \ x01t \ xd6 \ x16 \ xab` \ x00 \ xd2 $ \ xe1J \ xf8 \ xc9 ^ - \ x01B \ x01B \ t \ x00 \ x00 \ x00 \ x0f \ x05 \ xef \ x01 \ xf0 \ x01 \ x15 \ x04 \ xc8 \ x00E \ x01 \ x08 \ xb5 \ x00 \ x06 \ xb6 \ x00 \ x05B0 \ xff \ x18 \ x00 \ x00 \ xcdiX \ xce \ x04eC \ x0f \ x8eD \ x00 \ x00 \ x02 \ xf1 \ x00 \ x01 \ x17 \ xb6 \ x10 \ x00 \ x006 \ x10 \ x00 \ x01 '
但正确的信息应该是:
00 62 CA FE 01 16 00 0F 33 35 39 36 33 32 31 30 33 31 34 36 39 37 30 08 01 00 00 01 74 D6 16 AB 60 00 D2 24 E1 4A F8 C9 5E 2D 01 42 01 42 09 00 00 00 0F 05 EF 01 F0 01 15 04 C8 00 45 01 08 B5 00 06 B6 00 05 42 30 FF 18 00 00 CD 69 58 CE 04 65 43 0F 8E 44 00 00 02 F1 00 01 17 B6 10 00 00 36 10 00 01
我怎么能收到这种格式的,这是我的代码:
import socket
from ast import literal_eval
localIP = "192.168.0.30"
localPort = 12560
bufferSize = 1024
msgFromServer = "1"
bytesToSend = str.encode(msgFromServer)
# Create a datagram socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Bind to address and ip
UDPServerSocket.bind((localIP, localPort))
print("UDP server up and listening")
# Listen for incoming datagrams
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
message = bytesAddressPair[0]
address = bytesAddressPair[1]
clientMsg = "Message from Client:{}".format(message)
clientIP = "Client IP Address:{}".format(address)
#messajes= Encoding.ASCII.GetString(message)
print(clientMsg)
print(clientIP)
print(bytesAddressPair)
# Sending a reply to client
UDPServerSocket.sendto(bytesToSend, address)
感谢您的帮助
您正在以 python bytes 取回数据。如果您需要的话,可以使用 .hex() 方法将它们转换为十六进制字符串表示形式。例如:
data = b"\x00b \xca \fe \x01 \x0f359632103146970"
data.hex()
'006220ca200c652001200f333539363332313033313436393730'
socket.recvfrom(bufsize[, flags])
Receive data from the socket. The return value is a pair (bytes, address) where bytes is >a bytes object representing the data received and address is the address of the socket >sending the data. See the Unix manual page recv(2) for the meaning of the optional >argument flags; it defaults to zero. (The format of address depends on the address family >— see above.)"
你好朋友我在 python 有一个基于 UDP 协议的套接字服务器,当接收到 gps 设备发送的消息时,teltonika 向我显示以下内容:
'\ x00b \ xca \ xfe \ x01 \ x16 \ x00 \ x0f359632103146970 \ x08 \ x01 \ x00 \ x00 \ x01t \ xd6 \ x16 \ xab` \ x00 \ xd2 $ \ xe1J \ xf8 \ xc9 ^ - \ x01B \ x01B \ t \ x00 \ x00 \ x00 \ x0f \ x05 \ xef \ x01 \ xf0 \ x01 \ x15 \ x04 \ xc8 \ x00E \ x01 \ x08 \ xb5 \ x00 \ x06 \ xb6 \ x00 \ x05B0 \ xff \ x18 \ x00 \ x00 \ xcdiX \ xce \ x04eC \ x0f \ x8eD \ x00 \ x00 \ x02 \ xf1 \ x00 \ x01 \ x17 \ xb6 \ x10 \ x00 \ x006 \ x10 \ x00 \ x01 '
但正确的信息应该是:
00 62 CA FE 01 16 00 0F 33 35 39 36 33 32 31 30 33 31 34 36 39 37 30 08 01 00 00 01 74 D6 16 AB 60 00 D2 24 E1 4A F8 C9 5E 2D 01 42 01 42 09 00 00 00 0F 05 EF 01 F0 01 15 04 C8 00 45 01 08 B5 00 06 B6 00 05 42 30 FF 18 00 00 CD 69 58 CE 04 65 43 0F 8E 44 00 00 02 F1 00 01 17 B6 10 00 00 36 10 00 01
我怎么能收到这种格式的,这是我的代码:
import socket
from ast import literal_eval
localIP = "192.168.0.30"
localPort = 12560
bufferSize = 1024
msgFromServer = "1"
bytesToSend = str.encode(msgFromServer)
# Create a datagram socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Bind to address and ip
UDPServerSocket.bind((localIP, localPort))
print("UDP server up and listening")
# Listen for incoming datagrams
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
message = bytesAddressPair[0]
address = bytesAddressPair[1]
clientMsg = "Message from Client:{}".format(message)
clientIP = "Client IP Address:{}".format(address)
#messajes= Encoding.ASCII.GetString(message)
print(clientMsg)
print(clientIP)
print(bytesAddressPair)
# Sending a reply to client
UDPServerSocket.sendto(bytesToSend, address)
感谢您的帮助
您正在以 python bytes 取回数据。如果您需要的话,可以使用 .hex() 方法将它们转换为十六进制字符串表示形式。例如:
data = b"\x00b \xca \fe \x01 \x0f359632103146970"
data.hex()
'006220ca200c652001200f333539363332313033313436393730'
socket.recvfrom(bufsize[, flags])
Receive data from the socket. The return value is a pair (bytes, address) where bytes is >a bytes object representing the data received and address is the address of the socket >sending the data. See the Unix manual page recv(2) for the meaning of the optional >argument flags; it defaults to zero. (The format of address depends on the address family >— see above.)"