系统相关
首页 > 系统相关> > Python-Windows上的多播绑定上的绑定错误

Python-Windows上的多播绑定上的绑定错误

作者:互联网

我需要在Python应用程序中使用多播,在谷歌搜索了一下之后,我发现了有效的代码片段,这里是:

# UDP multicast examples, Hugo Vincent, 2005-05-14.
import socket
import sys
import struct

def send(data, port=50000, addr='239.192.1.100'):
    """send(data[, port[, addr]]) - multicasts a UDP datagram."""
    # Create the socket
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # Make the socket multicast-aware, and set TTL.
    s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20) # Change TTL (=20) to suit
    s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1)
    # Send the data
    s.sendto(data, (addr, port))

def recv(port=50000, addr="239.192.1.100", buf_size=1024):
    """recv([port[, addr[,buf_size]]]) - waits for a datagram and returns the data."""

    # Create the socket
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # Set some options to make it multicast-friendly
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    try:
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
    except AttributeError:
            pass # Some systems don't support SO_REUSEPORT
    s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20)

    # Bind to the port
    s.bind(('', port))

    # Set some more multicast options
    intf = socket.gethostbyname(socket.gethostname())
    s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton(intf))
    mreq = struct.pack("4sl", socket.inet_aton(addr), socket.INADDR_ANY)

    s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

    # Receive the data, then unregister multicast receive membership, then close the port
    data, sender_addr = s.recvfrom(buf_size)
    s.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP, socket.inet_aton(addr) + socket.inet_aton('0.0.0.0'))
    s.close()
    return data

if __name__=="__main__":
    if sys.argv[1] == "recv":
            print recv()
    else:
            send("a")

我对绑定和多播有问题.

据我了解,如果我绑定接收消息的套接字,在这种情况下,它将过滤通信. (”,port)表示我想接收来自此套接字和此端口的所有流量,而不管数据包的目标ip(与0.0.0.0相同),让我们将这种情况称为1.

如果我有bind((addr,port)),这也有效.我将收到所有目的IP为该多播组的数据包(当然,我也需要加入该多播组),这是情况2.

现在,正如我所说的,这两种方法都有效,但仅适用于Linux.

我在Windows机器上尝试了我的小程序,第一种情况有效,但是当我尝试另一种时,我得到了

    Traceback (most recent call last):
  File "test.py", line 51, in <module>
    print recv()
  File "test.py", line 32, in recv
    s.bind((addr, port))
  File "C:\Python27\lib\socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 10049] The requested address is not valid in its context

我不是Windows系统专家(我主要在Linux上进行开发),但是我很感兴趣,为什么我的代码仅在Windows系统(我使用Windows 7 btw)上失败并出现此错误.

解决方法:

正如PYMOTW Multicast articleCarl Cerecke in the comments所指出的,Windows中使用socket.INADDR_ANY将绑定到默认的多播地址,并且如果您具有多个接口,则Windows可能会选择错误的接口.

为了解决这个问题,您可以显式指定要从中接收多播消息的接口:

group = socket.inet_aton(multicast_group)
iface = socket.inet_aton('192.168.1.10') # listen for multicast packets on this interface.
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, group+iface)

您可以使用以下命令获取接口列表:

socket.gethostbyname_ex(socket.gethostname()) 
# ("PCName", [], ["169.254.80.80", "192.168.1.10"])

在上面的示例中,我们可能希望跳过第一个169.254链接本地地址,然后选择所需的192.168.1.10地址.

socket.gethostbyname_ex(socket.gethostname())[2][1]
# "192.168.1.10"

标签:networking,udp,python,multicast
来源: https://codeday.me/bug/20191031/1973573.html