我有一个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>
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()
答案 0 :(得分:2)
这不是错误,只是警告。您可以通过关闭会话来处理它。试试这个:
async def a():
payload = {}
url = "https://awebsiteisthere.com"
curl = AsyncioCurl()
data = await curl.get(url,payload)
print(data)
await curl.session.close() # this
答案 1 :(得分:0)
我刚刚在您的close()
脚本的末尾添加了test.py
import asyncio
from AsyncioCurl import AsyncioCurl
async def a():
payload = {}
url = "https://awebsiteisthere.com"
data = await AsyncioCurl().get(url,payload)
print(data)
await AsyncioCurl.close() #added this line
task = [
a()
]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(task))