Exemple #1
0
async def scpd_get(
        control_url: str,
        address: str,
        port: int,
        loop=None
) -> typing.Tuple[typing.Dict, bytes, typing.Optional[Exception]]:
    loop = loop or asyncio.get_event_loop_policy().get_event_loop()
    finished: asyncio.Future = asyncio.Future()
    packet = serialize_scpd_get(control_url, address)
    transport, protocol = await loop.create_connection(
        lambda: SCPDHTTPClientProtocol(packet, finished), address, port)
    assert isinstance(protocol, SCPDHTTPClientProtocol)
    error = None
    try:
        body, response_code, response_msg = await asyncio.wait_for(
            finished, 1.0)
    except asyncio.TimeoutError:
        error = UPnPError("get request timed out")
        body = b''
    except UPnPError as err:
        error = err
        body = protocol.response_buff
    finally:
        transport.close()
    if not error:
        try:
            return deserialize_scpd_get_response(body), body, None
        except ElementTree.ParseError as err:
            error = UPnPError(err)
    return {}, body, error
Exemple #2
0
async def scpd_get(
    control_url: str,
    address: str,
    port: int,
    loop: typing.Optional[asyncio.AbstractEventLoop] = None
) -> typing.Tuple[typing.Dict[str, typing.Any], bytes,
                  typing.Optional[Exception]]:
    loop = loop or asyncio.get_event_loop()
    packet = serialize_scpd_get(control_url, address)
    finished: 'asyncio.Future[typing.Tuple[bytes, bytes, int, bytes]]' = loop.create_future(
    )
    proto_factory: typing.Callable[
        [], SCPDHTTPClientProtocol] = lambda: SCPDHTTPClientProtocol(
            packet, finished)
    try:
        connect_tup: typing.Tuple[
            asyncio.BaseTransport,
            asyncio.BaseProtocol] = await loop.create_connection(
                proto_factory, address, port)
    except ConnectionError as err:
        return {}, b'', UPnPError(f"{err.__class__.__name__}({str(err)})")
    protocol = connect_tup[1]
    transport = connect_tup[0]
    assert isinstance(protocol, SCPDHTTPClientProtocol)

    error = None
    wait_task: typing.Awaitable[typing.Tuple[bytes, bytes, int,
                                             bytes]] = asyncio.wait_for(
                                                 protocol.finished,
                                                 1.0,
                                                 loop=loop)
    body = b''
    raw_response = b''
    try:
        raw_response, body, response_code, response_msg = await wait_task
    except asyncio.TimeoutError:
        error = UPnPError("get request timed out")
    except UPnPError as err:
        error = err
        raw_response = protocol.response_buff
    finally:
        transport.close()
    if not error:
        try:
            return deserialize_scpd_get_response(body), raw_response, None
        except Exception as err:
            error = UPnPError(err)

    return {}, raw_response, error
Exemple #3
0
 def test_deserialize_another_device(self):
     xml_bytes = b"<?xml version=\"1.0\"?>\n<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n<specVersion>\n<major>1</major>\n<minor>0</minor>\n</specVersion>\n<device>\n<deviceType>urn:schemas-upnp-org:device:InternetGatewayDevice:1</deviceType>\n<friendlyName>CGA4131COM</friendlyName>\n<manufacturer>Cisco</manufacturer>\n<manufacturerURL>http://www.cisco.com/</manufacturerURL>\n<modelDescription>CGA4131COM</modelDescription>\n<modelName>CGA4131COM</modelName>\n<modelNumber>CGA4131COM</modelNumber>\n<modelURL>http://www.cisco.com</modelURL>\n<serialNumber></serialNumber>\n<UDN>uuid:11111111-2222-3333-4444-555555555556</UDN>\n<UPC>CGA4131COM</UPC>\n<serviceList>\n<service>\n<serviceType>urn:schemas-upnp-org:service:Layer3Forwarding:1</serviceType>\n<serviceId>urn:upnp-org:serviceId:L3Forwarding1</serviceId>\n<SCPDURL>/Layer3ForwardingSCPD.xml</SCPDURL>\n<controlURL>/upnp/control/Layer3Forwarding</controlURL>\n<eventSubURL>/upnp/event/Layer3Forwarding</eventSubURL>\n</service>\n</serviceList>\n<deviceList>\n<device>\n<deviceType>urn:schemas-upnp-org:device:WANDevice:1</deviceType>\n<friendlyName>WANDevice:1</friendlyName>\n<manufacturer>Cisco</manufacturer>\n<manufacturerURL>http://www.cisco.com/</manufacturerURL>\n<modelDescription>CGA4131COM</modelDescription>\n<modelName>CGA4131COM</modelName>\n<modelNumber>CGA4131COM</modelNumber>\n<modelURL>http://www.cisco.com</modelURL>\n<serialNumber></serialNumber>\n<UDN>uuid:ebf5a0a0-1dd1-11b2-a92f-603d266f9915</UDN>\n<UPC>CGA4131COM</UPC>\n<serviceList>\n<service>\n<serviceType>urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1</serviceType>\n<serviceId>urn:upnp-org:serviceId:WANCommonIFC1</serviceId>\n<SCPDURL>/WANCommonInterfaceConfigSCPD.xml</SCPDURL>\n<controlURL>/upnp/control/WANCommonInterfaceConfig0</controlURL>\n<eventSubURL>/upnp/event/WANCommonInterfaceConfig0</eventSubURL>\n</service>\n</serviceList>\n<deviceList>\n    <device>\n        <deviceType>urn:schemas-upnp-org:device:WANConnectionDevice:1</deviceType>\n        <friendlyName>WANConnectionDevice:1</friendlyName>\n        <manufacturer>Cisco</manufacturer>\n        <manufacturerURL>http://www.cisco.com/</manufacturerURL>\n        <modelDescription>CGA4131COM</modelDescription>\n        <modelName>CGA4131COM</modelName>\n        <modelNumber>CGA4131COM</modelNumber>\n        <modelURL>http://www.cisco.com</modelURL>\n        <serialNumber></serialNumber>\n        <UDN>uuid:11111111-2222-3333-4444-555555555555</UDN>\n        <UPC>CGA4131COM</UPC>\n        <serviceList>\n       <service>\n           <serviceType>urn:schemas-upnp-org:service:WANIPConnection:1</serviceType>\n           <serviceId>urn:upnp-org:serviceId:WANIPConn1</serviceId>\n           <SCPDURL>/WANIPConnectionServiceSCPD.xml</SCPDURL>\n           <controlURL>/upnp/control/WANIPConnection0</controlURL>\n           <eventSubURL>/upnp/event/WANIPConnection0</eventSubURL>\n       </service>\n        </serviceList>\n    </device>\n</deviceList>\n</device>\n</deviceList>\n<presentationURL>http://10.1.10.1/</presentationURL></device>\n</root>\n"
     expected_parsed = {
         'specVersion': {
             'major': '1',
             'minor': '0'
         },
         'device': {
             'deviceType':
             'urn:schemas-upnp-org:device:InternetGatewayDevice:1',
             'friendlyName': 'CGA4131COM',
             'manufacturer': 'Cisco',
             'manufacturerURL': 'http://www.cisco.com/',
             'modelDescription': 'CGA4131COM',
             'modelName': 'CGA4131COM',
             'modelNumber': 'CGA4131COM',
             'modelURL': 'http://www.cisco.com',
             'UDN': 'uuid:11111111-2222-3333-4444-555555555556',
             'UPC': 'CGA4131COM',
             'serviceList': {
                 'service': {
                     'serviceType':
                     'urn:schemas-upnp-org:service:Layer3Forwarding:1',
                     'serviceId': 'urn:upnp-org:serviceId:L3Forwarding1',
                     'SCPDURL': '/Layer3ForwardingSCPD.xml',
                     'controlURL': '/upnp/control/Layer3Forwarding',
                     'eventSubURL': '/upnp/event/Layer3Forwarding'
                 }
             },
             'deviceList': {
                 'device': {
                     'deviceType':
                     'urn:schemas-upnp-org:device:WANDevice:1',
                     'friendlyName': 'WANDevice:1',
                     'manufacturer': 'Cisco',
                     'manufacturerURL': 'http://www.cisco.com/',
                     'modelDescription': 'CGA4131COM',
                     'modelName': 'CGA4131COM',
                     'modelNumber': 'CGA4131COM',
                     'modelURL': 'http://www.cisco.com',
                     'UDN': 'uuid:ebf5a0a0-1dd1-11b2-a92f-603d266f9915',
                     'UPC': 'CGA4131COM',
                     'serviceList': {
                         'service': {
                             'serviceType':
                             'urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1',
                             'serviceId':
                             'urn:upnp-org:serviceId:WANCommonIFC1',
                             'SCPDURL':
                             '/WANCommonInterfaceConfigSCPD.xml',
                             'controlURL':
                             '/upnp/control/WANCommonInterfaceConfig0',
                             'eventSubURL':
                             '/upnp/event/WANCommonInterfaceConfig0'
                         }
                     },
                     'deviceList': {
                         'device': {
                             'deviceType':
                             'urn:schemas-upnp-org:device:WANConnectionDevice:1',
                             'friendlyName': 'WANConnectionDevice:1',
                             'manufacturer': 'Cisco',
                             'manufacturerURL': 'http://www.cisco.com/',
                             'modelDescription': 'CGA4131COM',
                             'modelName': 'CGA4131COM',
                             'modelNumber': 'CGA4131COM',
                             'modelURL': 'http://www.cisco.com',
                             'UDN':
                             'uuid:11111111-2222-3333-4444-555555555555',
                             'UPC': 'CGA4131COM',
                             'serviceList': {
                                 'service': {
                                     'serviceType':
                                     'urn:schemas-upnp-org:service:WANIPConnection:1',
                                     'serviceId':
                                     'urn:upnp-org:serviceId:WANIPConn1',
                                     'SCPDURL':
                                     '/WANIPConnectionServiceSCPD.xml',
                                     'controlURL':
                                     '/upnp/control/WANIPConnection0',
                                     'eventSubURL':
                                     '/upnp/event/WANIPConnection0'
                                 }
                             }
                         }
                     }
                 }
             },
             'presentationURL': 'http://10.1.10.1/'
         }
     }
     self.assertDictEqual(expected_parsed,
                          deserialize_scpd_get_response(xml_bytes))
Exemple #4
0
 def test_fail_to_deserialize_invalid_root_xmls(self):
     with self.assertRaises(UPnPError):
         deserialize_scpd_get_response(self.response_bad_root_xmls)
Exemple #5
0
 def test_deserialize_blank(self):
     self.assertDictEqual(deserialize_scpd_get_response(b''), {})
Exemple #6
0
 def test_deserialize_get_response(self):
     self.assertDictEqual(deserialize_scpd_get_response(self.response),
                          self.expected_parsed)