Python socket.IPPROTO_IP Examples
The following are 30
code examples of socket.IPPROTO_IP().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
socket
, or try the search function
.
Example #1
Source File: multicast_checks.py From rift-python with Apache License 2.0 | 7 votes |
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)
Example #2
Source File: gateway.py From PyXiaomiGateway with MIT License | 7 votes |
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
Example #3
Source File: container.py From benchexec with Apache License 2.0 | 6 votes |
def activate_network_interface(iface): """Bring up the given network interface. @raise OSError: if interface does not exist or permissions are missing """ iface = iface.encode() sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP) try: # Get current interface flags from kernel ifreq = struct.pack( _STRUCT_IFREQ_LAYOUT_IFADDR_SAFAMILY, iface, socket.AF_INET, b"0" * 14 ) ifreq = fcntl.ioctl(sock, _SIOCGIFFLAGS, ifreq) if_flags = struct.unpack(_STRUCT_IFREQ_LAYOUT_IFFLAGS, ifreq)[1] # Set new flags ifreq = struct.pack( _STRUCT_IFREQ_LAYOUT_IFFLAGS, iface, if_flags | _IFF_UP, b"0" * 14 ) fcntl.ioctl(sock, _SIOCSIFFLAGS, ifreq) finally: sock.close()
Example #4
Source File: hello.py From ros2cli with Apache License 2.0 | 6 votes |
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
Example #5
Source File: test_protocol.py From exchangelib with BSD 2-Clause "Simplified" License | 6 votes |
def test_close(self): proc = psutil.Process() ip_addresses = {info[4][0] for info in socket.getaddrinfo( 'example.com', 80, socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP )} self.assertGreater(len(ip_addresses), 0) protocol = Protocol(config=Configuration( service_endpoint='http://example.com', credentials=Credentials('A', 'B'), auth_type=NOAUTH, version=Version(Build(15, 1)), retry_policy=FailFast() )) session = protocol.get_session() session.get('http://example.com') self.assertEqual(len({p.raddr[0] for p in proc.connections() if p.raddr[0] in ip_addresses}), 1) protocol.release_session(session) protocol.close() self.assertEqual(len({p.raddr[0] for p in proc.connections() if p.raddr[0] in ip_addresses}), 0)
Example #6
Source File: __init__.py From ros2cli with Apache License 2.0 | 6 votes |
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
Example #7
Source File: utils.py From iptvtools with MIT License | 6 votes |
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
Example #8
Source File: client.py From pyvpn with The Unlicense | 6 votes |
def ifconfig(dev, ipaddr, netmask): # http://stackoverflow.com/questions/6652384/how-to-set-the-ip-address-from-c-in-linux sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP) AF_INET = socket.AF_INET fd = sock.fileno() addrbuf = struct.pack('BBBB', *[int(el) for el in ipaddr.split('.')]) maskbuf = struct.pack('BBBB', *[int(el) for el in netmask.split('.')]) sockaddr_mt = '16sHH4s' flags_mt = '16sH' # ADDR siocsifaddr = struct.pack(sockaddr_mt, dev, AF_INET, 0, addrbuf) fcntl.ioctl(fd, SIOCSIFADDR, siocsifaddr) # MASK siocsifnetmask = struct.pack(sockaddr_mt, dev, AF_INET, 0, maskbuf) fcntl.ioctl(fd, SIOCSIFNETMASK, siocsifnetmask) # ifconfig tun0 up ifr2 = struct.pack(flags_mt, dev, 0) ifr_ret = fcntl.ioctl(fd, SIOCGIFFLAGS, ifr2) cur_flags = struct.unpack(flags_mt, ifr_ret)[1] flags = cur_flags | (IFF_UP | IFF_RUNNING) ifr_ret = struct.pack(flags_mt, dev, flags) ifr_ret = fcntl.ioctl(fd, SIOCSIFFLAGS, ifr_ret) return 0
Example #9
Source File: util.py From pyvpn with The Unlicense | 6 votes |
def ifconfig(dev, ipaddr, netmask): # http://stackoverflow.com/questions/6652384/how-to-set-the-ip-address-from-c-in-linux sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP) AF_INET = socket.AF_INET fd = sock.fileno() addrbuf = struct.pack('BBBB', *[int(el) for el in ipaddr.split('.')]) maskbuf = struct.pack('BBBB', *[int(el) for el in netmask.split('.')]) sockaddr_mt = '16sHH4s' flags_mt = '16sH' # ADDR siocsifaddr = struct.pack(sockaddr_mt, dev, AF_INET, 0, addrbuf) fcntl.ioctl(fd, SIOCSIFADDR, siocsifaddr) # MASK siocsifnetmask = struct.pack(sockaddr_mt, dev, AF_INET, 0, maskbuf) fcntl.ioctl(fd, SIOCSIFNETMASK, siocsifnetmask) # ifconfig tun0 up ifr2 = struct.pack(flags_mt, dev, 0) ifr_ret = fcntl.ioctl(fd, SIOCGIFFLAGS, ifr2) cur_flags = struct.unpack(flags_mt, ifr_ret)[1] flags = cur_flags | (IFF_UP | IFF_RUNNING) ifr_ret = struct.pack(flags_mt, dev, flags) ifr_ret = fcntl.ioctl(fd, SIOCSIFFLAGS, ifr_ret) return 0
Example #10
Source File: tuntest.py From pyvpn with The Unlicense | 6 votes |
def ifconfig(dev, ipaddr, netmask): # http://stackoverflow.com/questions/6652384/how-to-set-the-ip-address-from-c-in-linux sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP) AF_INET = socket.AF_INET fd = sock.fileno() addrbuf = struct.pack('BBBB', *[int(el) for el in ipaddr.split('.')]) maskbuf = struct.pack('BBBB', *[int(el) for el in netmask.split('.')]) sockaddr_mt = '16sHH4s' flags_mt = '16sH' # ADDR siocsifaddr = struct.pack(sockaddr_mt, dev, AF_INET, 0, addrbuf) fcntl.ioctl(fd, SIOCSIFADDR, siocsifaddr) # MASK siocsifnetmask = struct.pack(sockaddr_mt, dev, AF_INET, 0, maskbuf) fcntl.ioctl(fd, SIOCSIFNETMASK, siocsifnetmask) # ifconfig tun0 up ifr2 = struct.pack(flags_mt, dev, 0) ifr_ret = fcntl.ioctl(fd, SIOCGIFFLAGS, ifr2) cur_flags = struct.unpack(flags_mt, ifr_ret)[1] flags = cur_flags | (IFF_UP | IFF_RUNNING) ifr_ret = struct.pack(flags_mt, dev, flags) ifr_ret = fcntl.ioctl(fd, SIOCSIFFLAGS, ifr_ret) return 0
Example #11
Source File: pyvpn.py From pyvpn with The Unlicense | 6 votes |
def ifconfig(dev, ipaddr, netmask): # http://stackoverflow.com/questions/6652384/how-to-set-the-ip-address-from-c-in-linux sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP) AF_INET = socket.AF_INET fd = sock.fileno() addrbuf = struct.pack('BBBB', *[int(el) for el in ipaddr.split('.')]) maskbuf = struct.pack('BBBB', *[int(el) for el in netmask.split('.')]) sockaddr_mt = '16sHH4s' flags_mt = '16sH' # ADDR siocsifaddr = struct.pack(sockaddr_mt, dev, AF_INET, 0, addrbuf) fcntl.ioctl(fd, SIOCSIFADDR, siocsifaddr) # MASK siocsifnetmask = struct.pack(sockaddr_mt, dev, AF_INET, 0, maskbuf) fcntl.ioctl(fd, SIOCSIFNETMASK, siocsifnetmask) # ifconfig tun0 up ifr2 = struct.pack(flags_mt, dev, 0) ifr_ret = fcntl.ioctl(fd, SIOCGIFFLAGS, ifr2) cur_flags = struct.unpack(flags_mt, ifr_ret)[1] flags = cur_flags | (IFF_UP | IFF_RUNNING) ifr_ret = struct.pack(flags_mt, dev, flags) ifr_ret = fcntl.ioctl(fd, SIOCSIFFLAGS, ifr_ret) return 0
Example #12
Source File: listener.py From pulseaudio-dlna with GNU General Public License v3.0 | 6 votes |
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()')
Example #13
Source File: client.py From ParadoxIP150v2 with Eclipse Public License 1.0 | 6 votes |
def _socketpair_compat(): """TCP/IP socketpair including Windows support""" listensock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) listensock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listensock.bind(("127.0.0.1", 0)) listensock.listen(1) iface, port = listensock.getsockname() sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) sock1.setblocking(0) try: sock1.connect(("localhost", port)) except socket.error as err: if err.errno != errno.EINPROGRESS and err.errno != errno.EWOULDBLOCK and err.errno != EAGAIN: raise sock2, address = listensock.accept() sock2.setblocking(0) listensock.close() return (sock1, sock2)
Example #14
Source File: __init__.py From PyXiaomiGateway with MIT License | 6 votes |
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
Example #15
Source File: server_setting.py From sslyze with GNU Affero General Public License v3.0 | 6 votes |
def _do_dns_lookup(hostname: str, port: int) -> str: try: addr_infos = socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.IPPROTO_IP) except (socket.gaierror, IndexError, ConnectionError): raise ServerHostnameCouldNotBeResolved(f"Could not resolve {hostname}") family, socktype, proto, canonname, sockaddr = addr_infos[0] # By default use the first DNS entry, IPv4 or IPv6 tentative_ip_addr = sockaddr[0] # But try to use IPv4 if we have both IPv4 and IPv6 addresses, to work around buggy networks for family, socktype, proto, canonname, sockaddr in addr_infos: if family == socket.AF_INET: tentative_ip_addr = sockaddr[0] return tentative_ip_addr
Example #16
Source File: coap.py From pyShelly with MIT License | 6 votes |
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
Example #17
Source File: client.py From aws-iot-device-sdk-python with Apache License 2.0 | 6 votes |
def _socketpair_compat(): """TCP/IP socketpair including Windows support""" listensock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) listensock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listensock.bind(("127.0.0.1", 0)) listensock.listen(1) iface, port = listensock.getsockname() sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) sock1.setblocking(0) try: sock1.connect(("127.0.0.1", port)) except socket.error as err: if err.errno != errno.EINPROGRESS and err.errno != errno.EWOULDBLOCK and err.errno != EAGAIN: raise sock2, address = listensock.accept() sock2.setblocking(0) listensock.close() return (sock1, sock2)
Example #18
Source File: minusconf.py From scoop with GNU Lesser General Public License v3.0 | 6 votes |
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
Example #19
Source File: socket_handler.py From traceflow with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self, daddr): try: self.raw_sock = socket.socket( socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW ) except PermissionError as e: print(e) print("Please run as root!") exit(1) self.ip_daddr = daddr os_release = platform.system() if os_release == "Darwin": # Boned. TODO: Work on fixing this. print("Detected Mac OS - Cannot support writing of raw IP packets, exiting") exit(1) if os_release.endswith("BSD"): # BSD - Need to explicit set IP_HDRINCL. # BSD - Need to explicitly calculate IP total length self.raw_sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1) logging.debug("Detected a BSD") if os_release == "Linux": # Linux - No need to set IP_HDRINCL,as setting SOCK_RAW auto sets this. However should be explicit in settings. self.raw_sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1) logging.debug("Detected Linux") if os_release == "Windows": # No idea - No ability to test. Maybe abort? # TODO: Find testers? logging.debug("Detected NT") print("Untested on Windows - Exiting") exit(1)
Example #20
Source File: ssdp.py From AlexaControlledSamsungTV with MIT License | 5 votes |
def discover(service, timeout=5, retries=1, mx=3): group = ("239.255.255.250", 1900) message = "\r\n".join([ 'M-SEARCH * HTTP/1.1', 'HOST: {0}:{1}', 'MAN: "ssdp:discover"', 'ST: {st}', 'MX: {mx}', '', '']) socket.setdefaulttimeout(timeout) responses = {} for _ in range(retries): 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, 2) message_bytes = message.format( *group, st=service, mx=mx).encode('utf-8') sock.sendto(message_bytes, group) while True: try: response = SSDPResponse(sock.recv(1024)) responses[response.location] = response except socket.timeout: break return list(responses.values())
Example #21
Source File: main.py From uPyEcho with Apache License 2.0 | 5 votes |
def init_socket(self): ok = True self.ip = "239.255.255.250" self.port = 1900 try: # This is needed to join a multicast group self.mreq = struct.pack("4sl", inet_aton(self.ip), INADDR_ANY) # Set up server socket self.ssock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: self.ssock.bind(("", self.port)) except Exception as e: dbg("WARNING: Failed to bind %s:%d: %s", (self.ip, self.port, e)) ok = False try: dbg( "IP: " + str(socket.IPPROTO_IP) + " IP_ADD_MEMBERSHIP: " + str(socket.IP_ADD_MEMBERSHIP) + " mreq: " + str(self.mreq) ) self.ssock.setsockopt( socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, self.mreq ) except Exception as e: dbg("WARNING: Failed to join multicast group!: " + str(e)) ok = False except Exception as e: dbg("Failed to initialize UPnP sockets!") return False if ok: dbg("Listening for UPnP broadcasts")
Example #22
Source File: minusconf.py From scoop with GNU Lesser General Public License v3.0 | 5 votes |
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)
Example #23
Source File: minusconf.py From scoop with GNU Lesser General Public License v3.0 | 5 votes |
def _multicast_configure_sender(sock, ttl=None): if ttl != None: ttl_bin = struct.pack('@I', ttl) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin) if sock.family == socket.AF_INET6: sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl_bin)
Example #24
Source File: udp.py From learn_python3_spider with MIT License | 5 votes |
def getTTL(self): return self.socket.getsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL)
Example #25
Source File: ssdp.py From OpenBCI_Python with MIT License | 5 votes |
def discover(service, timeout=5, retries=1, mx=3, wifi_found_cb=None): group = ("239.255.255.250", 1900) message = "\r\n".join([ 'M-SEARCH * HTTP/1.1', 'HOST: {0}:{1}', 'MAN: "ssdp:discover"', 'ST: {st}', 'MX: {mx}', '', '']) socket.setdefaulttimeout(timeout) responses = {} for _ in range(retries): 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, 2) sockMessage = message.format(*group, st=service, mx=mx) if pyVersion == 3: sockMessage = sockMessage.encode("utf-8") sock.sendto(sockMessage, group) while True: try: response = SSDPResponse(sock.recv(1024)) if wifi_found_cb is not None: wifi_found_cb(response) responses[response.location] = response except socket.timeout: break return list(responses.values())
Example #26
Source File: udp.py From learn_python3_spider with MIT License | 5 votes |
def getOutgoingInterface(self): i = self.socket.getsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF) return socket.inet_ntoa(struct.pack("@i", i))
Example #27
Source File: udp.py From learn_python3_spider with MIT License | 5 votes |
def _setInterface(self, addr): i = socket.inet_aton(addr) self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, i) return 1
Example #28
Source File: udp.py From learn_python3_spider with MIT License | 5 votes |
def getLoopbackMode(self): return self.socket.getsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP)
Example #29
Source File: udp.py From learn_python3_spider with MIT License | 5 votes |
def setLoopbackMode(self, mode): mode = struct.pack("b", operator.truth(mode)) self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, mode)
Example #30
Source File: udp.py From learn_python3_spider with MIT License | 5 votes |
def setTTL(self, ttl): ttl = struct.pack("B", ttl) self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)