简体   繁体   中英

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. This works fine on Linux, but this method is not available on Windows (Python 3.3). How can I obtain the sender's IP address?

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

socket. recvfrom ( bufsize [, flags ])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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