diff --git a/aioupnp/__main__.py b/aioupnp/__main__.py index a00bb18..392747f 100644 --- a/aioupnp/__main__.py +++ b/aioupnp/__main__.py @@ -47,6 +47,7 @@ def main(): 'timeout': 1, 'service': '', # if not provided try all of them 'man': '', + 'mx': 1, 'return_as_json': True } @@ -80,8 +81,8 @@ def main(): UPnP.run_cli( command.replace('-', '_'), options.pop('lan_address'), options.pop('gateway_address'), - options.pop('timeout'), options.pop('service'), options.pop('man'), options.pop('interface'), - kwargs + options.pop('timeout'), options.pop('service'), options.pop('man'), options.pop('mx'), + options.pop('interface'), kwargs ) diff --git a/aioupnp/commands.py b/aioupnp/commands.py index 0263e1e..3e34722 100644 --- a/aioupnp/commands.py +++ b/aioupnp/commands.py @@ -3,7 +3,7 @@ from typing import Tuple, Union none_or_str = Union[None, str] -class SCPDCommands: +class SOAPCommands: def debug_commands(self) -> dict: raise NotImplementedError() diff --git a/aioupnp/gateway.py b/aioupnp/gateway.py index a18adfa..42ce641 100644 --- a/aioupnp/gateway.py +++ b/aioupnp/gateway.py @@ -3,7 +3,7 @@ import socket from typing import Dict, List, Union, Type from aioupnp.util import get_dict_val_case_insensitive, BASE_PORT_REGEX, BASE_ADDRESS_REGEX from aioupnp.constants import SPEC_VERSION, UPNP_ORG_IGD, SERVICE -from aioupnp.commands import SCPDCommands +from aioupnp.commands import SOAPCommands from aioupnp.device import Device, Service from aioupnp.protocols.ssdp import m_search from aioupnp.protocols.scpd import scpd_get @@ -90,7 +90,7 @@ class Gateway: self._unsupported_actions = {} self._registered_commands = {} - self.commands = SCPDCommands() + self.commands = SOAPCommands() def gateway_descriptor(self) -> dict: r = { @@ -129,9 +129,9 @@ class Gateway: @classmethod async def discover_gateway(cls, lan_address: str, gateway_address: str, timeout: int = 1, - service: str = UPNP_ORG_IGD, man: str = '', + service: str = UPNP_ORG_IGD, man: str = '', mx: int = 1, ssdp_socket: socket.socket = None, soap_socket: socket.socket = None): - datagram = await m_search(lan_address, gateway_address, timeout, service, man, ssdp_socket) + datagram = await m_search(lan_address, gateway_address, timeout, service, man, mx, ssdp_socket) gateway = cls(**datagram.as_dict()) await gateway.discover_commands(soap_socket) return gateway diff --git a/aioupnp/protocols/ssdp.py b/aioupnp/protocols/ssdp.py index b859e74..6f801d7 100644 --- a/aioupnp/protocols/ssdp.py +++ b/aioupnp/protocols/ssdp.py @@ -25,15 +25,15 @@ class SSDPProtocol(MulticastProtocol): self.notifications: List = [] self.replies: List = [] - def send_m_search_packet(self, service, address, man): + def send_m_search_packet(self, service, address, man, mx): packet = SSDPDatagram( SSDPDatagram._M_SEARCH, host="{}:{}".format(SSDP_IP_ADDRESS, SSDP_PORT), st=service, - man=man, mx=1 + man=man, mx=mx ) log.debug("sending packet to %s:%i: %s", address, SSDP_PORT, packet) self.transport.sendto(packet.encode().encode(), (address, SSDP_PORT)) - async def m_search(self, address, timeout: int = 1, service='', man='') -> SSDPDatagram: + async def m_search(self, address: str, timeout: int = 1, service: str = '', man: str = '', mx: int = 1) -> SSDPDatagram: if (address, service) in self.discover_callbacks: return self.discover_callbacks[(address, service)] man = man or SSDP_DISCOVER @@ -49,10 +49,10 @@ class SSDPProtocol(MulticastProtocol): # D-Link works with both # Cisco only works with quotes - self.send_m_search_packet(service, address, '\"%s\"' % man) + self.send_m_search_packet(service, address, '\"%s\"' % man, mx) # DD-WRT only works without quotes - self.send_m_search_packet(service, address, man) + self.send_m_search_packet(service, address, man, mx) f: Future = Future() f.add_done_callback(lambda _f: outer_fut.set_result(_f.result())) @@ -125,12 +125,12 @@ async def listen_ssdp(lan_address: str, gateway_address: str, async def m_search(lan_address: str, gateway_address: str, timeout: int = 1, - service: str = '', man: str = '', ssdp_socket: socket.socket = None) -> SSDPDatagram: + service: str = '', man: str = '', mx: int = 1, ssdp_socket: socket.socket = None) -> SSDPDatagram: transport, protocol, gateway_address, lan_address = await listen_ssdp( lan_address, gateway_address, ssdp_socket ) try: - return await protocol.m_search(address=gateway_address, timeout=timeout, service=service, man=man) + return await protocol.m_search(address=gateway_address, timeout=timeout, service=service, man=man, mx=mx) except asyncio.TimeoutError: raise UPnPError("M-SEARCH for {}:{} timed out".format(gateway_address, SSDP_PORT)) finally: diff --git a/aioupnp/upnp.py b/aioupnp/upnp.py index d491eb4..8a3b49d 100644 --- a/aioupnp/upnp.py +++ b/aioupnp/upnp.py @@ -42,23 +42,23 @@ class UPnP: @classmethod async def discover(cls, lan_address: str = '', gateway_address: str = '', timeout: int = 1, - service: str = '', man: str = '', interface_name: str = 'default', + service: str = '', man: str = '', mx: int = 1, interface_name: str = 'default', ssdp_socket: socket.socket = None, soap_socket: socket.socket = None): try: lan_address, gateway_address = cls.get_lan_and_gateway(lan_address, gateway_address, interface_name) except Exception as err: raise UPnPError("failed to get lan and gateway addresses: %s" % str(err)) gateway = await Gateway.discover_gateway( - lan_address, gateway_address, timeout, service, man, ssdp_socket, soap_socket + lan_address, gateway_address, timeout, service, man, mx, ssdp_socket, soap_socket ) return cls(lan_address, gateway_address, gateway) @classmethod @cli async def m_search(cls, lan_address: str = '', gateway_address: str = '', timeout: int = 1, - service: str = '', man: str = '', interface_name: str = 'default') -> Dict: + service: str = '', man: str = '', mx: int = 1, interface_name: str = 'default') -> Dict: lan_address, gateway_address = cls.get_lan_and_gateway(lan_address, gateway_address, interface_name) - datagram = await m_search(lan_address, gateway_address, timeout, service, man) + datagram = await m_search(lan_address, gateway_address, timeout, service, man, mx) return { 'lan_address': lan_address, 'gateway_address': gateway_address, @@ -216,7 +216,7 @@ class UPnP: @classmethod def run_cli(cls, method, lan_address: str = '', gateway_address: str = '', timeout: int = 60, - service: str = '', man: str = '', interface_name: str = 'default', + service: str = '', man: str = '', mx: int = 1, interface_name: str = 'default', kwargs: dict = None) -> None: kwargs = kwargs or {} timeout = int(timeout) @@ -231,12 +231,12 @@ class UPnP: async def wrapper(): if method == 'm_search': fn = lambda *_a, **_kw: cls.m_search( - lan_address, gateway_address, timeout, service, man, interface_name + lan_address, gateway_address, timeout, service, man, mx, interface_name ) else: try: u = await cls.discover( - lan_address, gateway_address, timeout, service, man, interface_name + lan_address, gateway_address, timeout, service, man, mx, interface_name ) except UPnPError as err: fut.set_exception(err)