當前位置: 首頁>>代碼示例>>Python>>正文


Python socket.IPV6_JOIN_GROUP屬性代碼示例

本文整理匯總了Python中socket.IPV6_JOIN_GROUP屬性的典型用法代碼示例。如果您正苦於以下問題:Python socket.IPV6_JOIN_GROUP屬性的具體用法?Python socket.IPV6_JOIN_GROUP怎麽用?Python socket.IPV6_JOIN_GROUP使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在socket的用法示例。


在下文中一共展示了socket.IPV6_JOIN_GROUP屬性的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _create_ipv6_sockets

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IPV6_JOIN_GROUP [as 別名]
def _create_ipv6_sockets(loopback_enabled):
    # Open a multicast send socket, with IP_MULTICAST_LOOP enabled or disabled as requested.
    intf_name = find_ethernet_interface()
    intf_index = socket.if_nametoindex(intf_name)
    mcast_address = "ff02::abcd:99"
    port = 30000
    group = (mcast_address, port)
    txsock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    txsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    txsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, intf_index)
    if loopback_enabled:
        txsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 1)
    else:
        txsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 0)
    txsock.connect(group)
    # Open a multicast receive socket and join the group
    rxsock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    req = struct.pack("=16si", socket.inet_pton(socket.AF_INET6, mcast_address), intf_index)
    if platform.system() == "Darwin":
        rxsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, req)
    else:
        rxsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_ADD_MEMBERSHIP, req)
    rxsock.bind(("::", port))
    return (txsock, rxsock) 
開發者ID:brunorijsman,項目名稱:rift-python,代碼行數:26,代碼來源:multicast_checks.py

示例2: join_ipv6_beacon_group

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IPV6_JOIN_GROUP [as 別名]
def join_ipv6_beacon_group(sock, ifindex):
    """Joins the MAAS IPv6 multicast group using the specified UDP socket.

    :param sock: An opened IPv6 UDP socket.
    :param ifindex: The interface index that should join the multicast group.
    """
    # XXX mpontillo 2017-06-21: Twisted doesn't support IPv6 here yet.
    # It would be nice to do this:
    # transport.joinGroup(BEACON_IPV6_MULTICAST)
    ipv6_join_sockopt_args = socket.inet_pton(
        socket.AF_INET6, BEACON_IPV6_MULTICAST
    ) + struct.pack("I", ifindex)
    try:
        sock.setsockopt(
            socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, ipv6_join_sockopt_args
        )
    except OSError:
        # Do this on a best-effort basis. We might get an "Address already in
        # use" error if the group is already joined, or (for whatever reason)
        # it is not possible to join a multicast group using this interface.
        pass 
開發者ID:maas,項目名稱:maas,代碼行數:23,代碼來源:services.py

示例3: _multicast_join_group

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IPV6_JOIN_GROUP [as 別名]
def _multicast_join_group(sock, family, addr):
    group_bin = _inet_pton(family, addr)
    if family == socket.AF_INET: # IPv4
        mreq = group_bin + struct.pack('=I', socket.INADDR_ANY)
        sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
    elif family == socket.AF_INET6: # IPv6
        mreq = group_bin + struct.pack('@I', 0)
        sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)
    else:
        raise ValueError('Unsupported protocol family ' + family) 
開發者ID:soravux,項目名稱:scoop,代碼行數:12,代碼來源:minusconf.py

示例4: ipv6Multicast

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IPV6_JOIN_GROUP [as 別名]
def ipv6Multicast(host='::1', maddr=maddr):
    haddr = socket.getaddrinfo(host, maddr[1], socket.AF_INET6, socket.SOCK_DGRAM)[0][-1]
    maddr = socket.getaddrinfo(maddr[0], maddr[1], socket.AF_INET6, socket.SOCK_DGRAM)[0][-1]

    sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)

    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    if hasattr(socket, "SO_REUSEPORT"):
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)

    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 1)
    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, 5)

    ifn = haddr[3] or if_nametoindex('lo0')
    ifn = struct.pack("I", ifn)
    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, ifn)

    group = socket.inet_pton(socket.AF_INET6, maddr[0]) + ifn
    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, group)

    sock.bind(haddr)
    sock.setblocking(False)

    return sock, maddr

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~ Main 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
開發者ID:ActiveState,項目名稱:code,代碼行數:30,代碼來源:recipe-442490.py

示例5: init_v6

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IPV6_JOIN_GROUP [as 別名]
def init_v6(self):
        idx = socket.if_nametoindex(self.interface.name)
        self.multicast_address = (WSD_MCAST_GRP_V6, WSD_UDP_PORT, 0x575C, idx)

        # v6: member_request = { multicast_addr, intf_idx }
        mreq = (
            socket.inet_pton(self.family, WSD_MCAST_GRP_V6) +
            struct.pack('@I', idx))
        self.recv_socket.setsockopt(
            socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)
        self.recv_socket.setsockopt(
            socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)

        # bind to network interface, i.e. scope and handle OS differences,
        # see Stevens: Unix Network Programming, Section 21.6, last paragraph
        try:
            self.recv_socket.bind((WSD_MCAST_GRP_V6, WSD_UDP_PORT, 0, idx))
        except OSError:
            self.recv_socket.bind(('::', 0, 0, idx))

        self.send_socket.setsockopt(
            socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 0)
        self.send_socket.setsockopt(
            socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, args.hoplimit)
        self.send_socket.setsockopt(
            socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, idx)

        self.transport_address = '[{0}]'.format(self.address)
        self.listen_address = (self.address, WSD_HTTP_PORT, 0, idx) 
開發者ID:christgau,項目名稱:wsdd,代碼行數:31,代碼來源:wsdd.py

示例6: receiver

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IPV6_JOIN_GROUP [as 別名]
def receiver(group):
    # Look up multicast group address in name server and find out IP version
    addrinfo = socket.getaddrinfo(group, None)[0]

    # Create a socket
    s = socket.socket(addrinfo[0], socket.SOCK_DGRAM)

    # Allow multiple copies of this program on one machine
    # (not strictly needed)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

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

    group_bin = socket.inet_pton(addrinfo[0], addrinfo[4][0])
    # Join group
    if addrinfo[0] == socket.AF_INET: # IPv4
        mreq = group_bin + struct.pack('=I', socket.INADDR_ANY)
        s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
    else:
        mreq = group_bin + struct.pack('@I', 0)
        s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)

    # Loop, printing any data we receive
    while True:
        data, sender = s.recvfrom(1500)
        while data[-1:] == '\0': data = data[:-1] # Strip trailing \0's
        print(str(sender) + '  ' + repr(data)) 
開發者ID:guohuadeng,項目名稱:odoo13-x64,代碼行數:30,代碼來源:mcast.py

示例7: create_socket_ipv6_rx_mcast

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IPV6_JOIN_GROUP [as 別名]
def create_socket_ipv6_rx_mcast(self):
        if self._interface_index is None:
            self.warning("Could not create IPv6 multicast receive socket: "
                         "unknown interface index")
            return None
        try:
            sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        except (IOError, OSError) as err:
            self.warning("Could not create IPv6 UDP socket: %s", err)
            return None
        self.enable_addr_and_port_reuse(sock)
        try:
            scoped_ipv6_multicast_address = (
                str(self._multicast_address) + '%' + self._interface_name)
            sockaddr = socket.getaddrinfo(scoped_ipv6_multicast_address,
                                          self._local_port,
                                          socket.AF_INET6,
                                          socket.SOCK_DGRAM)[0][4]
            sock.bind(sockaddr)
        except (IOError, OSError) as err:
            self.warning("Could not bind IPv6 UDP socket to address %s port %d: %s",
                         self._multicast_address, self._local_port, err)
            return None
        try:
            # pylint:disable=no-member
            req = struct.pack("=16si", socket.inet_pton(socket.AF_INET6, self._multicast_address),
                              self._interface_index)
            if MACOS:
                sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, req)
            else:
                sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_ADD_MEMBERSHIP, req)
        except (IOError, OSError) as err:
            self.warning("Could not join IPv6 group %s for interface index %s: %s",
                         self._multicast_address, self._interface_index, err)
            return None
        if not MACOS:
            try:
                # pylint:disable=no-member
                sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVPKTINFO, 1)
            except (IOError, OSError) as err:
                # Warn, but keep going; this socket option is not supported on macOS
                self.warning("Could not set IPV6_RECVPKTINFO socket option: %s", err)
        try:
            sock.setblocking(0)
        except (IOError, OSError) as err:
            self.warning("Could set multicast receive IPv6 UDP to non-blocking mode: %s", err)
            return None
        return sock 
開發者ID:brunorijsman,項目名稱:rift-python,代碼行數:50,代碼來源:udp_rx_handler.py

示例8: _joinMulticast

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IPV6_JOIN_GROUP [as 別名]
def _joinMulticast(self, addr, port, sock):
        import struct
        for res in socket.getaddrinfo(addr, port, socket.AF_UNSPEC, socket.SOCK_DGRAM):
            af, socktype, proto, canonname, sa = res
            break

        if af == socket.AF_INET6:
            num_colons = addr.count(':')
            new_colons = ':'
            for i in range(num_colons, 8):
                new_colons = '%s0:' % new_colons

            addr = addr.replace('::', new_colons)
            addr_pack = ''
            for l in addr.split(':'):
                word = int(l, 16)
                addr_pack = addr_pack + struct.pack('!H', word)

            interfaces = []
            for i in range(1, 32):
                try:
                    mreq = addr_pack + struct.pack('l', i)
                    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)
                    ok = True
                    self.log.debug('Joined IPv6 multicast on interface %d' % i)
                    interfaces.append(i)
                except Exception as e:
                    pass

            if len(interfaces) == 0:
                self.log.fatal('Could not join on any interface')
                raise Exception('Could not join multicast on any interface')
            return interfaces
        if af == socket.AF_INET:
            addr_pack = ''
            grpaddr = 0
            bytes = map(int, string.split(addr, '.'))
            for byte in bytes:
                grpaddr = grpaddr << 8 | byte

            ifaddr = socket.INADDR_ANY
            mreq = struct.pack('ll', socket.htonl(grpaddr), socket.htonl(ifaddr))
            try:
                self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
            except Exception as e:
                self.log.exception('Exception joining IPv4 multicast')

            return [] 
開發者ID:alesnav,項目名稱:p2ptv-pi,代碼行數:50,代碼來源:Multicast.py


注:本文中的socket.IPV6_JOIN_GROUP屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。