【发布时间】:2014-10-27 07:43:29
【问题描述】:
我打算使用 Python 将 UDP 字符串广播到本地网络。最直接的方法是什么?
【问题讨论】:
我打算使用 Python 将 UDP 字符串广播到本地网络。最直接的方法是什么?
【问题讨论】:
你可以使用socketpython模块
请看下面来自UdpComminicationwiki的代码
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
MESSAGE = "Hello, World!"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE.encode(), (UDP_IP, UDP_PORT))
【讨论】:
UDP target IP: 127.0.0.1 UDP target port: 5005 message: Hello, World!' ' Traceback (most recent call last): File "C:\Users\Adriano\Google Drive\python\socketTest.py", line 13, in <module> sock.sendto(MESSAGE, (UDP_IP, UDP_PORT)) TypeError: 'str' does not support the buffer interface