Python socket.IP_DROP_MEMBERSHIP Examples

The following are 13 code examples of socket.IP_DROP_MEMBERSHIP(). 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: __init__.py    From ros2cli with Apache License 2.0 6 votes vote down vote up
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 #2
Source File: hello.py    From ros2cli with Apache License 2.0 5 votes vote down vote up
def shutdown(self):
        if self._is_shutdown:
            return
        self._is_shutdown = True
        self._dummy_socket.sendto(
            f'{socket.gethostname()}'.encode('utf-8'), ('127.0.0.1', self._port)
        )
        self._dummy_socket.close()
        self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_DROP_MEMBERSHIP, self._mreq)
        self._socket.close() 
Example #3
Source File: udp.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def _joinAddr2(self, interface, addr, join):
        addr = socket.inet_aton(addr)
        interface = socket.inet_aton(interface)
        if join:
            cmd = socket.IP_ADD_MEMBERSHIP
        else:
            cmd = socket.IP_DROP_MEMBERSHIP
        try:
            self.socket.setsockopt(socket.IPPROTO_IP, cmd, addr + interface)
        except socket.error as e:
            return failure.Failure(error.MulticastJoinError(addr, interface,
                                                            *e.args)) 
Example #4
Source File: udp.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def _joinAddr2(self, interface, addr, join):
        addr = socket.inet_aton(addr)
        interface = socket.inet_aton(interface)
        if join:
            cmd = socket.IP_ADD_MEMBERSHIP
        else:
            cmd = socket.IP_DROP_MEMBERSHIP
        try:
            self.socket.setsockopt(socket.IPPROTO_IP, cmd, addr + interface)
        except socket.error as e:
            return failure.Failure(error.MulticastJoinError(addr, interface, *e.args)) 
Example #5
Source File: zeroconf.py    From apple_bleee with GNU General Public License v3.0 5 votes vote down vote up
def close(self):
        """Ends the background threads, and prevent this instance from
        servicing further queries."""
        if not self._GLOBAL_DONE:
            self._GLOBAL_DONE = True
            # remove service listeners
            self.remove_all_service_listeners()
            self.unregister_all_services()

            # shutdown recv socket and thread
            self.engine.del_reader(self._listen_socket)

            if self.address_family == socket.AF_INET:
                self._listen_socket.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP,
                    socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0'))
            else:
                group = socket.inet_pton(socket.AF_INET6,_MDNS_ADDR_IPV6) + self.ifn
                self._listen_socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_LEAVE_GROUP,group)
            
            self._listen_socket.close()
            self.engine.join()

            # shutdown the rest
            self.notify_all()
            self.reaper.join()
            for s in self._respond_sockets:
                s.close() 
Example #6
Source File: udp.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def _joinAddr2(self, interface, addr, join):
        addr = socket.inet_aton(addr)
        interface = socket.inet_aton(interface)
        if join:
            cmd = socket.IP_ADD_MEMBERSHIP
        else:
            cmd = socket.IP_DROP_MEMBERSHIP
        try:
            self.socket.setsockopt(socket.IPPROTO_IP, cmd, addr + interface)
        except socket.error as e:
            return failure.Failure(error.MulticastJoinError(addr, interface,
                                                            *e.args)) 
Example #7
Source File: udp.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def _joinAddr2(self, interface, addr, join):
        addr = socket.inet_aton(addr)
        interface = socket.inet_aton(interface)
        if join:
            cmd = socket.IP_ADD_MEMBERSHIP
        else:
            cmd = socket.IP_DROP_MEMBERSHIP
        try:
            self.socket.setsockopt(socket.IPPROTO_IP, cmd, addr + interface)
        except socket.error as e:
            return failure.Failure(error.MulticastJoinError(addr, interface, *e.args)) 
Example #8
Source File: ssdp.py    From script.tubecast with MIT License 5 votes vote down vote up
def server_close(self):
        self.handle_membership(socket.IP_DROP_MEMBERSHIP) 
Example #9
Source File: zeroconf.py    From HomePWN with GNU General Public License v3.0 5 votes vote down vote up
def close(self):
        """Ends the background threads, and prevent this instance from
        servicing further queries."""
        if not self._GLOBAL_DONE:
            self._GLOBAL_DONE = True
            # remove service listeners
            self.remove_all_service_listeners()
            self.unregister_all_services()

            # shutdown recv socket and thread
            self.engine.del_reader(self._listen_socket)

            if self.address_family == socket.AF_INET:
                self._listen_socket.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP,
                    socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0'))
            else:
                group = socket.inet_pton(socket.AF_INET6,_MDNS_ADDR_IPV6) + self.ifn
                self._listen_socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_LEAVE_GROUP,group)
            
            self._listen_socket.close()
            self.engine.join()

            # shutdown the rest
            self.notify_all()
            self.reaper.join()
            for s in self._respond_sockets:
                s.close() 
Example #10
Source File: udp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def _joinAddr2(self, interface, addr, join):
        addr = socket.inet_aton(addr)
        interface = socket.inet_aton(interface)
        if join:
            cmd = socket.IP_ADD_MEMBERSHIP
        else:
            cmd = socket.IP_DROP_MEMBERSHIP
        try:
            self.socket.setsockopt(socket.IPPROTO_IP, cmd, addr + interface)
        except socket.error, e:
            return failure.Failure(error.MulticastJoinError(addr, interface,
                                                            *e.args)) 
Example #11
Source File: udp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def _joinAddr2(self, interface, addr, join):
        addr = socket.inet_aton(addr)
        interface = socket.inet_aton(interface)
        if join:
            cmd = socket.IP_ADD_MEMBERSHIP
        else:
            cmd = socket.IP_DROP_MEMBERSHIP
        try:
            self.socket.setsockopt(socket.IPPROTO_IP, cmd, addr + interface)
        except socket.error, e:
            return failure.Failure(error.MulticastJoinError(addr, interface, *e.args)) 
Example #12
Source File: Zeroconf.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def close(self):
        """Ends the background threads, and prevent this instance from
        servicing further queries."""
        if globals()['_GLOBAL_DONE'] == 0:
            globals()['_GLOBAL_DONE'] = 1
            self.notifyAll()
            self.engine.notify()
            self.unregisterAllServices()
            self.socket.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0'))
            self.socket.close()
            
# Test a few module features, including service registration, service
# query (for Zoe), and service unregistration. 
Example #13
Source File: udp.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def _joinAddr2(self, interface, addr, join):
        addr = socket.inet_aton(addr)
        interface = socket.inet_aton(interface)
        if join:
            cmd = socket.IP_ADD_MEMBERSHIP
        else:
            cmd = socket.IP_DROP_MEMBERSHIP
        try:
            self.socket.setsockopt(socket.IPPROTO_IP, cmd, addr + interface)
        except socket.error, e:
            return failure.Failure(error.MulticastJoinError(addr, interface, *e.args))