def test_serialize_get(self): self.assertEqual(serialize_scpd_get(self.path, self.lan_address), self.get_request) self.assertEqual( serialize_scpd_get(self.path, 'http://' + self.lan_address), self.get_request) self.assertEqual( serialize_scpd_get(self.path, 'http://' + self.lan_address + ':1337'), self.get_request) self.assertEqual( serialize_scpd_get(self.path, self.lan_address + ':1337'), self.get_request)
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
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
def test_serialize_get(self): self.assertEqual(serialize_scpd_get(self.path, self.lan_address), self.get_request)
def test_serialize_get(self): self.assertEqual(serialize_scpd_get(self.path, self.lan_address), self.expected_result)