When I try to make a get or post call using aiohttp in a Quart endpoint it throws 'An existing connection was forcibly closed by the remote host', even when I follow the official tutorial here
Full exception stack:
Exception in callback _ProactorBasePipeTransport._call_connection_lost(None)
handle: <Handle _ProactorBasePipeTransport._call_connection_lost(None) created at C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\asyncio\proactor_events.py:109>
source_traceback: Object created at (most recent call last):
File "C:\Users\devuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\aiohttp\client.py", line 560, in _request
await resp.start(conn)
File "C:\Users\devuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\aiohttp\client_reqrep.py", line 917, in start
payload.on_eof(self._response_eof)
File "C:\Users\devuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\aiohttp\streams.py", line 167, in on_eof
callback()
File "C:\Users\devuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\aiohttp\client_reqrep.py", line 952, in _response_eof
self._connection.release()
File "C:\Users\devuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\aiohttp\connector.py", line 178, in release
self._connector._release(
File "C:\Users\devuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\aiohttp\connector.py", line 663, in _release
protocol.close()
File "C:\Users\devuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\aiohttp\client_proto.py", line 63, in close
transport.close()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\asyncio\proactor_events.py", line 109, in close
self._loop.call_soon(self._call_connection_lost, None)
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\asyncio\events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\asyncio\proactor_events.py", line 165, in _call_connection_lost
self._sock.shutdown(socket.SHUT_RDWR)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
I have noticed that I am not the only person who has reported this issue, I assume it's some interaction between aiohhtp and Quart that is breaking.
Asynchronous GET method:
async def _call_api_get(self, url : str, json_data : dict = None) -> ApiResponse:
try:
async with aiohttp.ClientSession() as session:
async with session.get(url, json=json_data) as resp:
body = await resp.json() \
if resp.content_type == 'application/json' \
else await resp.text()
api_return = ApiResponse(
status_code = resp.status,
body = body,
content_type = resp.content_type)
except ConnectionResetError as ex:
print("ConnectionResetError:", ex)
except Exception as ex:
api_return = ApiResponse(exception_msg = ex)
return api_return
The API endpoint method:
async def my_endpoint(self) -> Response:
auth_url = 'http://www.google.co.uk'
api_return_body : dict = {"key": "value"}
api_response = await self._call_api_get(auth_url,
json_data=auth_request)
Other related questions I looked at: