【问题标题】:Broadcast a UDP string with Python用 Python 广播一个 UDP 字符串
【发布时间】:2014-10-27 07:43:29
【问题描述】:

我打算使用 Python 将 UDP 字符串广播到本地网络。最直接的方法是什么?

【问题讨论】:

    标签: python udp broadcast


    【解决方案1】:

    你可以使用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))
    

    【讨论】:

    • 感谢您的即时答复。但是,脚本返回:'Traceback(最近一次调用最后一次):文件“C:\Users\Adriano\Google Drive\python\socketTest.py”,第 13 行,在 sock.sendto(MESSAGE, (UDP_IP, UDP_PORT)) TypeError: 'str' 不支持缓冲区接口'
    • 我应该补充一点:它是 Python 3.4,操作系统是 Windows 8.1
    • 请粘贴完整的回溯。
    • 这实际上是整个回溯,虽然格式很差。这是另一个尝试: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
    • 这里是解决方案stackoverflow.com/questions/11781639/… 也更新了我的答案。
    猜你喜欢
    • 2017-04-19
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    相关资源
    最近更新 更多