您不需要connect()到UDP套接字,而是:cs.sendto(data, ('255.255.255.255', 5455))
编辑:这似乎对我有用:from socket import *
cs = socket(AF_INET, SOCK_DGRAM)
cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
cs.sendto('This is a test', ('255.255.255.255', 54545))
在另一台机器上,我运行了tcpdump:tcpdump -i eth1 port 54545 -XX
listening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes
14:04:01.797259 IP 10.22.4.45.33749 > 255.255.255.255.54545: UDP, length 14
0x0000: ffff ffff ffff f0de f1c4 8aa6 0800 4500 ..............E.
0x0010: 002a 0000 4000 4011 2c81 0a16 042d ffff .*..@.@.,....-..
0x0020: ffff 83d5 d511 0016 fe38 5468 6973 2069 .........8This.i
0x0030: 7320 6120 7465 7374 0000 0000 s.a.test....
您可以看到有效负载中的文本。以及广播以太网和IP dst地址。