aiohttp: Unclosed client session client_session

11,622

It's not an error, just a warning. And you can deal with it by closing the session. Try this:

async def a():
    payload = {}
    url = "https://awebsiteisthere.com"
    curl = AsyncioCurl()
    data = await curl.get(url,payload)
    print(data)
    await curl.session.close()  # this
Share:
11,622

Related videos on Youtube

Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a test.py file and a AsyncioCurl.pyfile.
    I already use session instead of just aiohttp.request
    But it also give me this error:

    Unclosed client session
    client_session: <aiohttp.client.ClientSession object at 0x000001FAEFEA7DA0>
    Unclosed connector
    connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x000001FAF10AC648>, 119890.906)]']
    connector: <aiohttp.connector.TCPConnector object at 0x000001FAF0F702B0>
    

    test.py

    import asyncio
    from AsyncioCurl import AsyncioCurl
    async def a():
        payload = {}
        url = "https://awebsiteisthere.com"
        data = await AsyncioCurl().get(url,payload)
        print(data)
    
    task = [
        a()
    ]
    
    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.wait(task))
    

    AsyncioCurl.py

    import asyncio
    import aiohttp
    from Log import Log
    from Base import sign
    from config import config
    
    
    class AsyncioCurl:
        def __init__(self):
            self.session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=4))
    
        async def get(self,url,param):
            Log.debug("GET: "+url)
            payload = {
                "cookie":config["Token"]["COOKIE"]
            }
            payload = dict(param,**payload)
            payload = sign(payload)
            async with self.session.get(url,params=payload) as r:
                Log.debug(r.status)
                return await r.json()
    
        async def post(self,url,param):
            async with sem:
                Log.debug("POST: "+url)
                payload = {
                    "cookie":config["Token"]["COOKIE"]
                }
                payload = dict(param,**payload)
                payload = sign(payload)
                async with self.session.post(url,data=payload) as r:
                    return await r.json()
    
        async def nspost(self,url,param):
            Log.debug("POST: "+url)
            headers = {
                "Accept":"application/json, text/plain, */*",
                "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36",
                "Accept-Language":"zh-CN,zh;q=0.9",
                "accept-encoding":"gzip, deflate",
                "cookie":config["Token"]["COOKIE"]
            }
            async with self.session.post(url,data=param,headers=headers) as r:
                return await r.json()
    
        async def nsdpost(self,url):
            Log.debug("POST: "+url)
            headers = {
                "Accept":"application/json, text/plain, */*",
                "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36",
                "Accept-Language":"zh-CN,zh;q=0.9",
                "accept-encoding":"gzip, deflate",
                "cookie":config["Token"]["COOKIE"]
            }
            async with self.session.post(url,headers=headers) as r:
                return await r.json()
    
  • Ced
    Ced about 4 years
    for me await curl.close() #without session did the job