本文整理汇总了Python中socket.IP_ADD_MEMBERSHIP属性的典型用法代码示例。如果您正苦于以下问题:Python socket.IP_ADD_MEMBERSHIP属性的具体用法?Python socket.IP_ADD_MEMBERSHIP怎么用?Python socket.IP_ADD_MEMBERSHIP使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类socket
的用法示例。
在下文中一共展示了socket.IP_ADD_MEMBERSHIP属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _create_ipv4_sockets
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [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: run
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [as 别名]
def run(self, ttl=None):
if self.DISABLE_SSDP_LISTENER:
return
self.allow_reuse_address = True
SocketServer.UDPServer.__init__(
self, (self.host or '', self.SSDP_PORT), SSDPHandler)
self.socket.setsockopt(
socket.IPPROTO_IP,
socket.IP_ADD_MEMBERSHIP,
self._multicast_struct(self.SSDP_ADDRESS))
self.socket.setsockopt(
socket.IPPROTO_IP,
socket.IP_MULTICAST_TTL,
self.SSDP_TTL)
if ttl:
GObject.timeout_add(ttl * 1000, self.shutdown)
setproctitle.setproctitle('ssdp_listener')
self.serve_forever(self)
logger.info('SSDPListener.run()')
示例3: receive
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [as 别名]
def receive(*, group=DEFAULT_GROUP, port=DEFAULT_PORT, timeout=None):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
try:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except AttributeError:
# not available on Windows
pass
s.bind(('', port))
s.settimeout(timeout)
mreq = struct.pack('4sl', socket.inet_aton(group), socket.INADDR_ANY)
s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
try:
data, sender_addr = s.recvfrom(4096)
finally:
s.setsockopt(socket.IPPROTO_IP, socket.IP_DROP_MEMBERSHIP, mreq)
finally:
s.close()
return data, sender_addr
示例4: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [as 别名]
def __init__(self, summary_table, group=DEFAULT_GROUP, port=DEFAULT_PORT, timeout=None):
self._dummy_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
try:
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except AttributeError:
# not available on Windows
pass
self._socket.bind(('', port))
self._socket.settimeout(timeout)
self._mreq = struct.pack('4sl', socket.inet_aton(group), socket.INADDR_ANY)
self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, self._mreq)
except Exception:
self._dummy_socket.close()
self._socket.close()
raise
self._is_shutdown = False
self._summary_table = summary_table
self._group = group
self._port = port
示例5: check_udp_connectivity
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [as 别名]
def check_udp_connectivity(url, timeout=None):
"""Check UDP connectivity."""
ipaddr, port = url.rsplit(':', 1)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.settimeout(timeout)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.bind(('', int(port)))
mreq = struct.pack("4sl", socket.inet_aton(ipaddr), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
try:
if sock.recv(10240):
return True
except socket.timeout:
pass
return False
示例6: _create_mcast_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [as 别名]
def _create_mcast_socket(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if self._interface != 'any':
if platform.system() != "Windows":
sock.bind((self.MULTICAST_ADDRESS, self.MULTICAST_PORT))
else:
sock.bind((self._interface, self.MULTICAST_PORT))
mreq = socket.inet_aton(self.MULTICAST_ADDRESS) + socket.inet_aton(self._interface)
else:
if platform.system() != "Windows":
sock.bind((self.MULTICAST_ADDRESS, self.MULTICAST_PORT))
else:
sock.bind(('', self.MULTICAST_PORT))
mreq = struct.pack("=4sl", socket.inet_aton(self.MULTICAST_ADDRESS), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
return sock
示例7: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [as 别名]
def __init__(self, config, event_loop):
main_protocol = MainProtocol(config)
task = event_loop.create_datagram_endpoint(lambda: main_protocol,
local_addr=(config['ip'], 9898))
asyncio.ensure_future(task, loop=event_loop)
sock = socket.socket(
socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('224.0.0.50', 4321))
mreq = socket.inet_aton('224.0.0.50') + socket.inet_aton(config['ip'])
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
discovery_protocol = DiscoveryProtocol(config, main_protocol)
task = event_loop.create_datagram_endpoint(lambda: discovery_protocol,
sock=sock)
asyncio.ensure_future(task, loop=event_loop)
self.discovery_protocol = discovery_protocol
self.main_protocol = main_protocol
示例8: _init_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [as 别名]
def _init_socket(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 10)
sock.bind((self._root.bind_ip, COAP_PORT))
if self._root.host_ip:
mreq = struct.pack("=4s4s",
socket.inet_aton(COAP_IP),
socket.inet_aton(self._root.host_ip))
else:
mreq = struct.pack("=4sl",
socket.inet_aton(COAP_IP),
socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
sock.settimeout(15)
self._socket = sock
示例9: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [as 别名]
def __init__(self, group="239.255.255.250", port=1900):
self.group, self.port = group, port
self.callbacks = {}
# multicast socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
logger.debug("[SSDP] bind: 0.0.0.0:%s" % port)
sock.bind(('0.0.0.0', port))
mreq = struct.pack("=4sl", socket.inet_aton(group), socket.INADDR_ANY)
logger.debug("[SSDP] add membership: UDP/%s" % group)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
self.listening = False
self.sock = sock
self.devices = {}
# Start listening
示例10: init_v4
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [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)
示例11: create_sockets
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [as 别名]
def create_sockets(mcst_ipaddr, mcst_port):
mreq = struct.pack("4sl", socket.inet_aton(mcst_ipaddr),
socket.INADDR_ANY)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
socket.IPPROTO_UDP)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
if platform.system() == "Windows":
s.bind(("", mcst_port))
return (s, )
else:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
s.bind((mcst_ipaddr, mcst_port))
bsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
socket.IPPROTO_UDP)
bsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
bsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
bsock.bind(("", 1901))
return (s, bsock)
示例12: bind_to_multicast
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [as 别名]
def bind_to_multicast(port=45454, multicast_group='239.255.43.21'):
import struct
# Create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Bind to the server address
# sock.bind((multicast_group, port))
sock.bind(('', port))
# Tell the operating system to add the socket to the multicast group
# on all interfaces.
# group = socket.inet_aton(multicast_group)
# mreq = struct.pack('4sL', group, socket.INADDR_ANY)
mreq = struct.pack('4sl', socket.inet_aton(multicast_group), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
return sock
示例13: addListener
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [as 别名]
def addListener(self, addr, port, service):
if self.isBroadcast(addr):
self.etherAddrs[addr] = self.broadcastIpToMac(addr)
elif self.isMulticast(addr):
self.etherAddrs[addr] = self.multicastIpToMac(addr)
else:
# unicast -- we don't know yet which IP we'll want to send to
self.etherAddrs[addr] = None
# Set up the receiving socket and corresponding IP and interface information.
# One receiving socket is required per multicast address.
rx = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
rx.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
for interface in self.interfaces:
(ifname, mac, ip, netmask) = self.getInterface(interface)
# Add this interface to the receiving socket's list.
if self.isBroadcast(addr):
rx.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
elif self.isMulticast(addr):
packedAddress = struct.pack('4s4s', socket.inet_aton(addr), socket.inet_aton(ip))
rx.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, packedAddress)
# Generate a transmitter socket. Each interface
# requires its own transmitting socket.
if interface not in self.noTransmitInterfaces:
tx = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
tx.bind((ifname, 0))
self.transmitters.append({'relay': {'addr': addr, 'port': port}, 'interface': ifname, 'addr': ip, 'mac': mac, 'netmask': netmask, 'socket': tx, 'service': service})
rx.bind((addr, port))
self.receivers.append(rx)
示例14: create_socket_ipv4_rx_mcast
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [as 别名]
def create_socket_ipv4_rx_mcast(self):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
except (IOError, OSError) as err:
self.warning("Could not create IPv4 UDP socket: %s", err)
return None
self.enable_addr_and_port_reuse(sock)
try:
sock.bind((self._multicast_address, self._local_port))
except (IOError, OSError) as err:
self.warning("Could not bind IPv4 UDP socket to address %s port %d: %s",
self._multicast_address, self._local_port, err)
return None
if sock is None:
return None
if self._local_ipv4_address:
req = struct.pack("=4s4s", socket.inet_aton(self._multicast_address),
socket.inet_aton(self._local_ipv4_address))
else:
req = struct.pack("=4sl", socket.inet_aton(self._multicast_address), socket.INADDR_ANY)
try:
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, req)
except (IOError, OSError) as err:
self.warning("Could not join IPv4 group %s for local address %s: %s",
self._multicast_address, self._local_ipv4_address, err)
return None
if not MACOS:
try:
# pylint:disable=no-member
sock.setsockopt(socket.IPPROTO_IP, socket.IP_PKTINFO, 1)
except (IOError, OSError) as err:
# Warn, but keep going; this socket option is not supported on macOS
self.warning("Could not set IP_PKTINFO socket option: %s", err)
try:
sock.setblocking(0)
except (IOError, OSError) as err:
self.warning("Could set multicast receive IPv4 UDP to non-blocking mode: %s", err)
return None
return sock
示例15: getfiles
# 需要导入模块: import socket [as 别名]
# 或者: from socket import IP_ADD_MEMBERSHIP [as 别名]
def getfiles(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.settimeout(3)
sock.bind((self.mcast_grp, self.mcast_port))
mreq = struct.pack("=4sl", socket.inet_aton(self.mcast_grp), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
loop = True
chunk = {}
chunk["end"] = 0
N = 335
#Wait for an end chunk to start by the beginning
while not (chunk["end"]):
chunk = self._getchunk(sock)
firstfile = str(chunk["filetype"])+"_"+str(chunk["fileid"])
#Loop until firstfile
while (loop):
xmldata=""
chunk = self._getchunk(sock)
#Discard headers
body=chunk["data"]
while not (chunk["end"]):
xmldata+=body
chunk = self._getchunk(sock)
body=chunk["data"]
#Discard last 4bytes binary footer?
xmldata+=body[:-4]
self._files[str(chunk["filetype"])+"_"+str(chunk["fileid"])]=xmldata
N = N - 1
if (str(chunk["filetype"])+"_"+str(chunk["fileid"]) == firstfile or N == 0):
loop = False
sock.close()