本文整理汇总了Python中socket.IPV6_MULTICAST_LOOP属性的典型用法代码示例。如果您正苦于以下问题:Python socket.IPV6_MULTICAST_LOOP属性的具体用法?Python socket.IPV6_MULTICAST_LOOP怎么用?Python socket.IPV6_MULTICAST_LOOP使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类socket
的用法示例。
在下文中一共展示了socket.IPV6_MULTICAST_LOOP属性的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _create_ipv6_sockets
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPV6_MULTICAST_LOOP [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)
示例2: _init_advertiser
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPV6_MULTICAST_LOOP [as 别名]
def _init_advertiser(self):
sock = _find_sock()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, struct.pack('@I', 1))
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, struct.pack('@I', 1))
if sock.family == socket.AF_INET6:
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, struct.pack('@I', 1))
sock.bind(('', self.port))
addrs = _resolve_addrs(self.addresses, None, self.ignore_unavailable, (sock.family,))
for fam,to,orig_fam,orig_addr in addrs:
try:
_multicast_join_group(sock, orig_fam, orig_addr)
except socket.error:
if not self.ignore_unavailable:
raise
self._sock = sock
示例3: create_socket_ipv6_tx_mcast
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPV6_MULTICAST_LOOP [as 别名]
def create_socket_ipv6_tx_mcast(self, multicast_address, port, loopback):
if self._interface_index is None:
self.warning("Could not create IPv6 multicast TX socket: unknown interface index")
return None
try:
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
except IOError as err:
self.warning("Could not create IPv6 UDP socket: %s", err)
return None
self.enable_addr_and_port_reuse(sock)
try:
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, self._interface_index)
except IOError as err:
self.warning("Could not set IPv6 multicast interface index %d: %s",
self._interface_index, err)
return None
try:
loop_value = 1 if loopback else 0
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, loop_value)
except IOError as err:
self.warning("Could not set IPv6 multicast loopback value %d: %s", loop_value, err)
return None
try:
scoped_ipv6_multicast_address = (
str(multicast_address) + '%' + self.physical_interface_name)
sock_addr = socket.getaddrinfo(scoped_ipv6_multicast_address, port,
socket.AF_INET6, socket.SOCK_DGRAM)[0][4]
sock.connect(sock_addr)
except (IOError, OSError) as err:
self.warning("Could not connect UDP socket to address %s port %d: %s",
scoped_ipv6_multicast_address, port, err)
return None
return sock
示例4: ipv6Multicast
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPV6_MULTICAST_LOOP [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
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
示例5: init_v6
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPV6_MULTICAST_LOOP [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)
示例6: set_ipv6_multicast_loopback
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPV6_MULTICAST_LOOP [as 别名]
def set_ipv6_multicast_loopback(sock, loopback):
"""Sets IPv6 multicast loopback mode on the specified UDP socket.
This isn't used in MAAS under normal circumstances, but it is useful for
testing.
:param sock: An opened IPv6 UDP socket.
:param loopback: If True, will set up the socket to loop back transmitted
multicast to the receive path.
"""
sock.setsockopt(
socket.IPPROTO_IPV6,
socket.IPV6_MULTICAST_LOOP,
struct.pack("I", 1 if loopback else 0),
)
示例7: new_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IPV6_MULTICAST_LOOP [as 别名]
def new_socket(interface_number=None, interface_name=None):
"""
Create a new socket for a given interface_number.
interface_name and interface_number have to be for the same interface
"""
#Default is IPv4
address_family = socket.AF_INET
intf = None #The interface used to transmit data
if interface_number is None:
#No ipv6 interface given. Stay in ipv4
intf = socket.gethostbyname(socket.gethostname())
else: #IPv6
intf = int(interface_number)
address_family = socket.AF_INET6
#Create socket
s = socket.socket(address_family, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# SO_REUSEADDR should be equivalent to SO_REUSEPORT for
# multicast UDP sockets (p 731, "TCP/IP Illustrated,
# Volume 2"), but some BSD-derived systems require
# SO_REUSEPORT to be specified explicity. Also, not all
# versions of Python have SO_REUSEPORT available.
# Catch OSError and socket.error for kernel versions <3.9 because lacking
# SO_REUSEPORT support.
try:
reuseport = socket.SO_REUSEPORT
except AttributeError:
pass
else:
try:
s.setsockopt(socket.SOL_SOCKET, reuseport, 1)
except (OSError, socket.error) as err:
# OSError on python 3, socket.error on python 2
if not err.errno == errno.ENOPROTOOPT:
raise
# OpenBSD needs the ttl and loop values for the IP_MULTICAST_TTL and
# IP_MULTICAST_LOOP socket options as an unsigned char.
if address_family is socket.AF_INET:
ttl = struct.pack(b'B', 255)
s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
loop = struct.pack(b'B', 1)
s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, loop)
else: #IPv6
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, 255)
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 1)
if interface_name is "awdl0" and platform.system() == 'Darwin':
s.setsockopt(socket.SOL_SOCKET, 0x1104, 1)
#Bind the Socket
s.bind(('', _MDNS_PORT))
return s