[英]how to send UDP data/packet using Socket programming without converting it into bytes using python?
I tried to create a UDP socket and was successful in creating one.我尝试创建一个 UDP 套接字并成功创建了一个。 Now when I tried to send some raw data,
socket.send
wants me to convert it into bytes.现在,当我尝试发送一些原始数据时,
socket.send
要我将其转换为字节。 But this raw data is a command to change the time of my application which I am working on, so i wanted to send the data as it is.但是这个原始数据是改变我正在处理的应用程序时间的命令,所以我想按原样发送数据。 Is there a way to send this without converting it into bytes?
有没有办法在不将其转换为字节的情况下发送它?
here's the code i used:这是我使用的代码:
Socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
bufferSize = 1024
recvBuf = 4096
new_pkt= x00\x1c\x7fb\xb5\xfd\x00PV\xb8\x08\x9f\x08\x00E\x00\x000B\xad\x00\x00\x80\x11\x00\x00\n\xe7\xa0\xc6\n\xe7\x922\xc0\xb8\x05\xdc\x00\x1cH\xf4\t\x8d\x01\x00\x01\x01\x00\x10\x00\x00\x01,\x00\x00\x00\x004\xe6\xc0
S = Socket.connect_ex(("10.231.146.50",port))
Socket.settimeout(10)
Res = Socket.sendall(new_pkt)
Socket.close
obtained an error to convert the packet into bytes, while trying to send it在尝试发送数据包时,获得了将数据包转换为字节的错误
I get no error with Python2.7 , for send or sendall, if I put double quotes around your data string Python2.7没有错误,对于 send 或 sendall,如果我在你的数据字符串周围加上双引号
new_pkt= "x00\x1c\x7fb\xb5\xfd\x00PV\xb8\x08\x9f\x08\x00E\x00\x000B\xad\x00\x00\x80\x11\x00\x00\n\xe7\xa0\xc6\n\xe7\x922\xc0\xb8\x05\xdc\x00\x1cH\xf4\t\x8d\x01\x00\x01\x01\x00\x10\x00\x00\x01,\x00\x00\x00\x004\xe6\xc0"
Give it a try ;-)试一试 ;-)
If you use Python3.x , you need bytes.如果您使用Python3.x ,则需要字节。 So you just prefix your string with a
b
:所以你只需用
b
前缀你的字符串:
new_pkt_for_py3 = b"x00\x1c\x7fb\xb5\xfd...."
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.