diff --git a/aioupnp/__main__.py b/aioupnp/__main__.py index 990d714..a00bb18 100644 --- a/aioupnp/__main__.py +++ b/aioupnp/__main__.py @@ -2,7 +2,6 @@ import logging import sys from aioupnp.upnp import UPnP - log = logging.getLogger("aioupnp") handler = logging.StreamHandler() handler.setFormatter(logging.Formatter('%(asctime)-15s-%(filename)s:%(lineno)s->%(message)s')) @@ -47,6 +46,7 @@ def main(): 'lan_address': '', 'timeout': 1, 'service': '', # if not provided try all of them + 'man': '', 'return_as_json': True } @@ -80,7 +80,7 @@ def main(): UPnP.run_cli( command.replace('-', '_'), options.pop('lan_address'), options.pop('gateway_address'), - options.pop('timeout'), options.pop('service'), options.pop('interface'), + options.pop('timeout'), options.pop('service'), options.pop('man'), options.pop('interface'), kwargs ) diff --git a/aioupnp/gateway.py b/aioupnp/gateway.py index 4e746dd..b2d00eb 100644 --- a/aioupnp/gateway.py +++ b/aioupnp/gateway.py @@ -1,7 +1,6 @@ import logging import socket -import typing -from typing import Dict, List, Union, Type, Tuple +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 @@ -130,15 +129,15 @@ class Gateway: @classmethod async def discover_gateway(cls, lan_address: str, gateway_address: str, timeout: int = 1, - service: str = UPNP_ORG_IGD, ssdp_socket: socket.socket = None, - soap_socket: socket.socket = None): - datagram = await m_search(lan_address, gateway_address, timeout, service, ssdp_socket) + service: str = UPNP_ORG_IGD, man: str = '', + ssdp_socket: socket.socket = None, soap_socket: socket.socket = None): + datagram = await m_search(lan_address, gateway_address, timeout, service, man, ssdp_socket) gateway = cls(**datagram.as_dict()) await gateway.discover_commands(soap_socket) return gateway async def discover_commands(self, soap_socket: socket.socket = None): - response = await scpd_get("/" + self.path.decode(), self.base_ip.decode(), self.port) + response = await scpd_get(self.path.decode(), self.base_ip.decode(), self.port) self.spec_version = get_dict_val_case_insensitive(response, SPEC_VERSION) self.url_base = get_dict_val_case_insensitive(response, "urlbase") diff --git a/aioupnp/protocols/ssdp.py b/aioupnp/protocols/ssdp.py index 4e99e47..dc26c56 100644 --- a/aioupnp/protocols/ssdp.py +++ b/aioupnp/protocols/ssdp.py @@ -33,10 +33,10 @@ class SSDPProtocol(MulticastProtocol): 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='') -> SSDPDatagram: + async def m_search(self, address, timeout: int = 1, service='', man='') -> SSDPDatagram: if (address, service) in self.discover_callbacks: return self.discover_callbacks[(address, service)] - + man = man or SSDP_DISCOVER if not service: services = [UPNP_ORG_IGD, WIFI_ALLIANCE_ORG_IGD] else: @@ -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\"' % SSDP_DISCOVER) + self.send_m_search_packet(service, address, '\"%s\"' % man) # DD-WRT only works without quotes - self.send_m_search_packet(service, address, SSDP_DISCOVER) + self.send_m_search_packet(service, address, man) f: Future = Future() f.add_done_callback(lambda _f: outer_fut.set_result(_f.result())) @@ -119,12 +119,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 = '', ssdp_socket: socket.socket = None) -> SSDPDatagram: + service: str = '', man: str = '', 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) + return await protocol.m_search(address=gateway_address, timeout=timeout, service=service, man=man) 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 5458706..fd7c5bc 100644 --- a/aioupnp/upnp.py +++ b/aioupnp/upnp.py @@ -42,21 +42,23 @@ class UPnP: @classmethod async def discover(cls, lan_address: str = '', gateway_address: str = '', timeout: int = 1, - service: str = '', interface_name: str = 'default', + service: str = '', man: str = '', 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, ssdp_socket, soap_socket) + gateway = await Gateway.discover_gateway( + lan_address, gateway_address, timeout, service, man, 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 = '', interface_name: str = 'default') -> Dict: + service: str = '', man: str = '', 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) + datagram = await m_search(lan_address, gateway_address, timeout, service, man) return { 'lan_address': lan_address, 'gateway_address': gateway_address, @@ -214,10 +216,9 @@ class UPnP: @classmethod def run_cli(cls, method, lan_address: str = '', gateway_address: str = '', timeout: int = 60, - service: str = '', interface_name: str = 'default', + service: str = '', man: str = '', interface_name: str = 'default', kwargs: dict = None) -> None: kwargs = kwargs or {} - try: asyncio.get_running_loop() except RuntimeError: @@ -228,10 +229,12 @@ class UPnP: async def wrapper(): if method == 'm_search': - fn = lambda *_a, **_kw: cls.m_search(lan_address, gateway_address, timeout, service, interface_name) + fn = lambda *_a, **_kw: cls.m_search( + lan_address, gateway_address, timeout, service, man, interface_name + ) else: u = await cls.discover( - lan_address, gateway_address, timeout, service, interface_name + lan_address, gateway_address, timeout, service, man, interface_name ) if hasattr(u, method) and hasattr(getattr(u, method), "_cli"): fn = getattr(u, method)