2019-07-29 09:59:10 +08:00
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
|
|
|
import re
|
|
|
|
|
|
|
|
RESPONSE_BIN = 0
|
|
|
|
RESPONSE_TEXT = 1
|
|
|
|
RESPONSE_JSON = 2
|
|
|
|
RESPONSE_FILE = 3
|
2024-04-06 13:43:28 +08:00
|
|
|
RESPONSE_STREAM = 4
|
2019-07-29 09:59:10 +08:00
|
|
|
|
|
|
|
class HttpClient:
|
|
|
|
def __init__(self,coding='utf-8'):
|
|
|
|
self.coding = coding
|
|
|
|
self.session = None
|
|
|
|
self.cookies = {}
|
|
|
|
|
|
|
|
async def close(self):
|
2024-02-28 17:53:44 +08:00
|
|
|
if self.session:
|
|
|
|
await self.session.close()
|
|
|
|
self.session = None
|
2019-07-29 09:59:10 +08:00
|
|
|
|
|
|
|
def url2domain(self,url):
|
|
|
|
parts = url.split('/')[:3]
|
|
|
|
pre = '/'.join(parts)
|
|
|
|
return pre
|
|
|
|
|
|
|
|
def setCookie(self,url,cookies):
|
|
|
|
name = self.url2domain(url)
|
|
|
|
self.cookies[name] = cookies
|
|
|
|
|
|
|
|
def getCookies(self,url):
|
|
|
|
name = url2domain(url)
|
|
|
|
return self.cookies.get(name,None)
|
|
|
|
|
|
|
|
def getsession(self,url):
|
|
|
|
if self.session is None:
|
|
|
|
jar = aiohttp.CookieJar(unsafe=True)
|
|
|
|
self.session = aiohttp.ClientSession(cookie_jar=jar)
|
|
|
|
return self.session
|
|
|
|
|
2024-04-06 13:43:28 +08:00
|
|
|
async def handleResp(self,url,resp,resp_type, stream_func=None):
|
2019-07-29 09:59:10 +08:00
|
|
|
if resp.cookies is not None:
|
|
|
|
self.setCookie(url,resp.cookies)
|
|
|
|
|
|
|
|
if resp_type == RESPONSE_BIN:
|
|
|
|
return await resp.read()
|
|
|
|
if resp_type == RESPONSE_JSON:
|
|
|
|
return await resp.json()
|
2024-04-06 13:43:28 +08:00
|
|
|
if resp_type == RESPONSE_TEXT:
|
|
|
|
return await resp.text(self.coding)
|
|
|
|
async for chunk in resp.content.iter_chunked(1024):
|
|
|
|
if stream_func:
|
2024-04-07 18:11:13 +08:00
|
|
|
await stream_func(chunk)
|
2019-07-29 09:59:10 +08:00
|
|
|
|
|
|
|
def grapCookie(self,url):
|
|
|
|
session = self.getsession(url)
|
|
|
|
domain = self.url2domain(url)
|
|
|
|
filtered = session.cookie_jar.filter_cookies(domain)
|
|
|
|
return filtered
|
|
|
|
|
2024-02-29 14:15:25 +08:00
|
|
|
async def request(self, url, method,
|
2024-02-29 18:49:03 +08:00
|
|
|
response_type=RESPONSE_TEXT,
|
2024-02-29 14:15:25 +08:00
|
|
|
params=None,
|
|
|
|
data=None,
|
2024-03-03 19:55:00 +08:00
|
|
|
jd=None,
|
2024-04-06 13:43:28 +08:00
|
|
|
stream_func=None,
|
2024-02-29 14:15:25 +08:00
|
|
|
headers=None,
|
|
|
|
**kw):
|
2019-07-29 09:59:10 +08:00
|
|
|
session = self.getsession(url)
|
2024-03-03 19:55:00 +08:00
|
|
|
if params == {}:
|
|
|
|
params = None
|
|
|
|
if data == {}:
|
|
|
|
data = None
|
|
|
|
if jd == {}:
|
|
|
|
jd = None
|
|
|
|
if headers == {}:
|
|
|
|
headers = None
|
|
|
|
|
2024-02-29 14:15:25 +08:00
|
|
|
resp = await session.request(method, url,
|
|
|
|
params=params,
|
|
|
|
data=data,
|
2024-03-03 19:55:00 +08:00
|
|
|
json=jd,
|
2024-02-29 14:15:25 +08:00
|
|
|
headers=headers, **kw)
|
2019-07-29 09:59:10 +08:00
|
|
|
if resp.status==200:
|
2024-04-06 13:43:28 +08:00
|
|
|
return await self.handleResp(url, resp, response_type, stream_func=stream_func)
|
2024-03-03 19:55:00 +08:00
|
|
|
raise Exception(f'http error({resp.status}, {url=},{params=}, {data=}, {jd=}, {headers=}, {kw=})')
|
2019-07-29 09:59:10 +08:00
|
|
|
|
2024-02-29 14:15:25 +08:00
|
|
|
async def get(self,url,**kw):
|
|
|
|
return self.request(url, 'GET', **kw)
|
|
|
|
|
|
|
|
async def post(self,url, **kw):
|
|
|
|
return self.request(url, 'POST', **kw)
|
2019-07-29 09:59:10 +08:00
|
|
|
session = self.getsession(url)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
async def gbaidu(hc):
|
|
|
|
r = await hc.get('https://www.baidu.com')
|
|
|
|
print(r)
|
|
|
|
await hc.close()
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
hc = HttpClient()
|
|
|
|
loop.run_until_complete(gbaidu(hc))
|
|
|
|
|