简体   繁体   English

Python:在Windows上获取UDP广播/ socket.recvcmsg()的发送者

[英]Python: Get sender of UDP broadcast / socket.recvcmsg() on Windows

I use socket.recvmsg() to get the data and the sender's IP address of a UDP broadcast message. 我使用socket.recvmsg()来获取数据和UDP广播消息的发送者的IP地址。 This works fine on Linux, but this method is not available on Windows (Python 3.3). 这在Linux上可以正常工作,但在Windows(Python 3.3)上不可用。 How can I obtain the sender's IP address? 如何获取发送者的IP地址?

from socket import *

fd = socket(AF_INET, SOCK_DGRAM)
fd.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
fd.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)

data, _, _, addr = fd.recvmsg(4096) # <- how to achieve this without recvmsg?
host, upd_port =  addr

I noticed in the Python documentation that you linked, that recvfrom returns similar values to what your code is looking for (bytes, address) and is compatible with Windows 我在您链接的Python文档中注意到, recvfrom返回与您的代码正在寻找的值(字节,地址)相似的值,并且与Windows兼容

socket. 插座。 recvfrom ( bufsize [, flags ]) recvfrombufsize [, 标志 ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM