aio-libs / aiohttp

Asynchronous HTTP client/server framework for asyncio and Python
https://docs.aiohttp.org
Other
15.12k stars 2.02k forks source link

aiohttp.client_exceptions.ClientConnectionError: Connection closed #6995

Closed IIDarkNessYT closed 2 years ago

IIDarkNessYT commented 2 years ago

Describe the bug

My code:

class request():

    @classmethod
    async def get(self, url, call, *, content=None, headers=None):
        if content is not None:
            if headers is not None:
                async with call.request(method='GET', url=url, json=content, headers=headers) as response:
                    return response
            else:
                async with call.request(method='GET', url=url, json=content) as response:
                    return response
        else:
            if headers is not None:
                async with call.request(method='GET', url=url, headers=headers) as response:
                    return response
            else:
                async with call.request(method='GET', url=url) as response:
                    return response

async def check_premium(guild, owner):
    async with aiohttp.ClientSession() as call:
        jsonstr = {
            "MY": "CONTENT"
        }
        url = "MY URL"
        result = await request.get(url, call, content=jsonstr)
        print(await result.json())

async def test():
    await check_premium('995048257447280770', '553960665581355009')

loop = asyncio.get_event_loop()
loop.run_until_complete(test())

To Reproduce

  1. I use client
  2. I'm run my classmethod in function

Expected behavior

-

Logs/tracebacks

Traceback (most recent call last):
  File "func.py", line 89, in <module>
    loop.run_until_complete(test())
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "func.py", line 86, in test
    await check_premium('995048257447280770', '553960665581355009')
  File "func.py", line 74, in check_premium
    print(await result.json())
  File "/home/darkness/.local/lib/python3.8/site-packages/aiohttp/client_reqrep.py", line 1092, in json
    await self.read()
  File "/home/darkness/.local/lib/python3.8/site-packages/aiohttp/client_reqrep.py", line 1032, in read
    self._body = await self.content.read()
  File "/home/darkness/.local/lib/python3.8/site-packages/aiohttp/streams.py", line 344, in read
    raise self._exception
aiohttp.client_exceptions.ClientConnectionError: Connection closed

Python Version

Python 3.8.10

aiohttp Version

Name: aiohttp
Version: 3.7.4.post0
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: fafhrd91@gmail.com
License: Apache 2
Location: /home/darkness/.local/lib/python3.8/site-packages
Requires: chardet, attrs, async-timeout, typing-extensions, yarl, multidict
Required-by: py-cord, discord.py, discord-py-interactions, dhooks

multidict Version

Name: multidict
Version: 6.0.2
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: /home/darkness/.local/lib/python3.8/site-packages
Requires: 
Required-by: yarl, aiohttp

yarl Version

Name: yarl
Version: 1.7.2
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: /home/darkness/.local/lib/python3.8/site-packages
Requires: multidict, idna
Required-by: aiohttp

OS

Linux Mint xfce

Related component

Client

Additional context

No response

Code of Conduct

Dreamsorcerer commented 2 years ago

Maybe you're not familiar with context managers, but you're using async with:

                async with call.request(method='GET', url=url, json=content, headers=headers) as response:
                    return response

Which means when the function returns, the request context is exited and the connection is closed. You won't be able to read anything after that point (and shouldn't really be using the response object at all).

Recommendation is to avoid passing response objects around, it's just likely to cause you more problems. Either return the JSON result directly from that method, or otherwise rework the code.