本文整理匯總了Python中socket.IP_MULTICAST_LOOP屬性的典型用法代碼示例。如果您正苦於以下問題:Python socket.IP_MULTICAST_LOOP屬性的具體用法?Python socket.IP_MULTICAST_LOOP怎麽用?Python socket.IP_MULTICAST_LOOP使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類socket
的用法示例。
在下文中一共展示了socket.IP_MULTICAST_LOOP屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _create_ipv4_sockets
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_MULTICAST_LOOP [as 別名]
def _create_ipv4_sockets(loopback_enabled):
# Open a multicast send socket, with IP_MULTICAST_LOOP enabled or disabled as requested.
mcast_address = "224.0.1.195"
port = 49501
group = (mcast_address, port)
txsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
txsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if loopback_enabled:
txsock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1)
else:
txsock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)
txsock.connect(group)
# Open a multicast receive socket and join the group
rxsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
req = struct.pack("=4sl", socket.inet_aton(mcast_address), socket.INADDR_ANY)
rxsock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, req)
rxsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
rxsock.bind(group)
return (txsock, rxsock)
示例2: _create_ipv6_sockets
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_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)
示例3: _init_advertiser
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_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
示例4: init_v4
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_MULTICAST_LOOP [as 別名]
def init_v4(self):
idx = socket.if_nametoindex(self.interface.name)
self.multicast_address = (WSD_MCAST_GRP_V4, WSD_UDP_PORT)
# v4: member_request (ip_mreqn) = { multicast_addr, intf_addr, idx }
mreq = (
socket.inet_pton(self.family, WSD_MCAST_GRP_V4) +
socket.inet_pton(self.family, self.address) +
struct.pack('@I', idx))
self.recv_socket.setsockopt(
socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
try:
self.recv_socket.bind((WSD_MCAST_GRP_V4, WSD_UDP_PORT))
except OSError:
self.recv_socket.bind(('', WSD_UDP_PORT))
self.send_socket.setsockopt(
socket.IPPROTO_IP, socket.IP_MULTICAST_IF, mreq)
self.send_socket.setsockopt(
socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)
self.send_socket.setsockopt(
socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, args.hoplimit)
self.listen_address = (self.address, WSD_HTTP_PORT)
示例5: _loopback_needed
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_MULTICAST_LOOP [as 別名]
def _loopback_needed(ipv6):
# If we receive a single copy when loopback is disabled, then we need to disable loopback
if _copies_received(ipv6, False) == 1:
return False
# We receive zero copies with loopback disabled. Make sure we do receive a single copy with
# loopback enabled.
copies = _copies_received(ipv6, True)
if copies == 1:
return True
elif copies == 0:
assert False, "We don't receive multicast packet with or without IP_MULTICAST_LOOP"
else:
assert False, "Unexpected IP_MULTICAST_LOOP behavior"
return False # To make Pylint happy
示例6: create_socket_ipv4_tx_mcast
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_MULTICAST_LOOP [as 別名]
def create_socket_ipv4_tx_mcast(self, multicast_address, port, loopback):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
except IOError as err:
self.warning("Could not create IPv4 UDP socket: %s", err)
return None
self.enable_addr_and_port_reuse(sock)
if self._ipv4_address is not None:
try:
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF,
socket.inet_aton(self._ipv4_address))
except IOError as err:
self.warning("Could not set IPv6 multicast interface address %s: %s",
self._ipv4_address, err)
return None
try:
loop_value = 1 if loopback else 0
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, loop_value)
except IOError as err:
self.warning("Could not set IPv4 multicast loopback value %d: %s", loop_value, err)
return None
try:
sock.connect((multicast_address, port))
except IOError as err:
self.warning("Could not connect UDP socket to address %s port %d: %s",
multicast_address, port, err)
return None
return sock
示例7: _server_discovery
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_MULTICAST_LOOP [as 別名]
def _server_discovery(self):
MULTI_GROUP = ("<broadcast>", 7359)
MESSAGE = "who is EmbyServer?"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(1.0) # This controls the socket.timeout exception
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1)
sock.setsockopt(socket.IPPROTO_IP, socket.SO_REUSEADDR, 1)
LOG.debug("MultiGroup : %s", str(MULTI_GROUP))
LOG.debug("Sending UDP Data: %s", MESSAGE)
servers = []
try:
sock.sendto(MESSAGE, MULTI_GROUP)
except Exception as error:
LOG.error(error)
return servers
while True:
try:
data, addr = sock.recvfrom(1024) # buffer size
servers.append(json.loads(data))
except socket.timeout:
LOG.info("Found Servers: %s", servers)
return servers
except Exception as e:
LOG.error("Error trying to find servers: %s", e)
return servers
示例8: receiver
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_MULTICAST_LOOP [as 別名]
def receiver(service='mihome'):
from plugins import gateway
assert service in MULTICAST, 'No such service'
store = get_store()
address, port = MULTICAST.get(service)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("0.0.0.0", port))
mreq = struct.pack("=4sl", socket.inet_aton(address), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, SOCKET_BUFSIZE)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
sock.settimeout(20) # 2x of heartbeat period
current = {}
while True:
try:
data, _ = sock.recvfrom(SOCKET_BUFSIZE) # buffer size is 1024 bytes
except socket.timeout:
continue
print(datetime.now().isoformat(), data)
if service == 'mihome':
message = json.loads(data.decode())
data = json.loads(message['data'])
if message.get('model') in ('sensor_ht', 'weather.v1') and not sensor_ht.process(conn, cursor, current, message, data):
continue
elif message.get('model') == 'magnet':
magnet.process(store, message, data)
elif message.get('model') == 'gateway':
gateway.process(store, message, data)
current = {}
elif service == 'yeelight':
yeelight.process(data.decode())
示例9: getLoopbackMode
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_MULTICAST_LOOP [as 別名]
def getLoopbackMode(self):
return self.socket.getsockopt(socket.IPPROTO_IP,
socket.IP_MULTICAST_LOOP)
示例10: setLoopbackMode
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_MULTICAST_LOOP [as 別名]
def setLoopbackMode(self, mode):
mode = struct.pack("b", operator.truth(mode))
self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP,
mode)
示例11: getLoopbackMode
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_MULTICAST_LOOP [as 別名]
def getLoopbackMode(self):
return self.socket.getsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP)
示例12: setLoopbackMode
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_MULTICAST_LOOP [as 別名]
def setLoopbackMode(self, mode):
mode = struct.pack("b", operator.truth(mode))
self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, mode)
示例13: create_multicast_sock
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_MULTICAST_LOOP [as 別名]
def create_multicast_sock(own_ip, remote_addr, bind_to_multicast_addr):
"""Create UDP multicast socket."""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(False)
sock.setsockopt(
socket.SOL_IP,
socket.IP_MULTICAST_IF,
socket.inet_aton(own_ip))
sock.setsockopt(
socket.SOL_IP,
socket.IP_ADD_MEMBERSHIP,
socket.inet_aton(remote_addr[0]) +
socket.inet_aton(own_ip))
sock.setsockopt(
socket.IPPROTO_IP,
socket.IP_MULTICAST_TTL, 2)
sock.setsockopt(
socket.IPPROTO_IP,
socket.IP_MULTICAST_IF,
socket.inet_aton(own_ip))
# I have no idea why we have to use different bind calls here
# - bind() with multicast addr does not work with gateway search requests
# on some machines. It only works if called with own ip. It also doesn't
# work on Mac OS.
# - bind() with own_ip does not work with ROUTING_INDICATIONS on Gira
# knx router - for an unknown reason.
if bind_to_multicast_addr:
if platform == "win32":
sock.bind(('', remote_addr[1]))
else:
sock.bind((remote_addr[0], remote_addr[1]))
else:
sock.bind((own_ip, 0))
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)
return sock
示例14: _server_discovery
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_MULTICAST_LOOP [as 別名]
def _server_discovery(self):
MULTI_GROUP = ("<broadcast>", 7359)
MESSAGE = b"who is JellyfinServer?"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(1.0) # This controls the socket.timeout exception
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1)
sock.setsockopt(socket.IPPROTO_IP, socket.SO_REUSEADDR, 1)
LOG.debug("MultiGroup : %s", str(MULTI_GROUP))
LOG.debug("Sending UDP Data: %s", MESSAGE)
servers = []
try:
sock.sendto(MESSAGE, MULTI_GROUP)
except Exception as error:
LOG.exception(traceback.format_exc())
LOG.exception(error)
return servers
while True:
try:
data, addr = sock.recvfrom(1024) # buffer size
servers.append(json.loads(data))
except socket.timeout:
LOG.info("Found Servers: %s", servers)
return servers
except Exception as e:
LOG.error(traceback.format_exc())
LOG.exception("Error trying to find servers: %s", e)
return servers
示例15: set_loopback_mode
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import IP_MULTICAST_LOOP [as 別名]
def set_loopback_mode(self, mode):
mode = struct.pack("b", operator.truth(mode))
self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP,
mode)