gpt4 book ai didi

python - aiohttp:未关闭的客户端 session client_session

转载 作者:行者123 更新时间:2023-12-02 07:27:11 50 4
gpt4 key购买 nike

我有一个 test.py 文件和一个 AsyncioCurl.py 文件。
我已经使用 session 而不仅仅是 aiohttp.request
但它也给了我这个错误:

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>

测试.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()

最佳答案

这不是错误,只是警告。您可以通过关闭 session 来处理它。试试这个:

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

关于python - aiohttp:未关闭的客户端 session client_session,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54807599/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com