async def scpd_post( control_url: str, address: str, port: int, method: str, param_names: list, service_id: bytes, loop: typing.Optional[asyncio.AbstractEventLoop] = None, **kwargs: typing.Dict[str, typing.Any] ) -> typing.Tuple[typing.Dict, bytes, typing.Optional[Exception]]: loop = loop or asyncio.get_event_loop() finished: 'asyncio.Future[typing.Tuple[bytes, bytes, int, bytes]]' = loop.create_future( ) packet = serialize_soap_post(method, param_names, service_id, address.encode(), control_url.encode(), **kwargs) proto_factory: typing.Callable[[], SCPDHTTPClientProtocol] = lambda:\ SCPDHTTPClientProtocol(packet, finished, soap_method=method, soap_service_id=service_id.decode()) 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) try: wait_task: typing.Awaitable[typing.Tuple[bytes, bytes, int, bytes]] = asyncio.wait_for( finished, 1.0, loop=loop) raw_response, body, response_code, response_msg = await wait_task except asyncio.TimeoutError: return {}, b'', UPnPError("Timeout") except UPnPError as err: return {}, protocol.response_buff, err finally: transport.close() try: return (deserialize_soap_post_response(body, method, service_id.decode()), raw_response, None) except Exception as err: return {}, raw_response, UPnPError(err)
async def scpd_post( control_url: str, address: str, port: int, method: str, param_names: list, service_id: bytes, loop=None, **kwargs ) -> 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_soap_post(method, param_names, service_id, address.encode(), control_url.encode(), **kwargs) transport, protocol = await loop.create_connection( lambda: SCPDHTTPClientProtocol( packet, finished, soap_method=method, soap_service_id=service_id.decode(), ), address, port) assert isinstance(protocol, SCPDHTTPClientProtocol) try: body, response_code, response_msg = await asyncio.wait_for( finished, 1.0) except asyncio.TimeoutError: return {}, b'', UPnPError("Timeout") except UPnPError as err: return {}, protocol.response_buff, err finally: transport.close() try: return (deserialize_soap_post_response(body, method, service_id.decode()), body, None) except (ElementTree.ParseError, UPnPError) as err: return {}, body, UPnPError(err)
def test_serialize_post_http_host(self): self.assertEqual( serialize_soap_post(self.method, self.param_names, self.st, b'http://' + self.gateway_address, self.path, **self.kwargs), self.post_bytes)
def test_serialize_get(self): self.assertEqual( serialize_soap_post(self.method, self.param_names, self.st, self.gateway_address, self.path, **self.kwargs), self.expected_result)