This commit is contained in:
yumoqing 2025-03-06 01:02:28 +08:00
parent e5d72b67bf
commit ec090a205e

View File

@ -1,3 +1,4 @@
import os
import asyncio import asyncio
import aiohttp import aiohttp
from aiohttp import FormData from aiohttp import FormData
@ -39,7 +40,6 @@ class HttpClient:
self.coding = coding self.coding = coding
self.session = None self.session = None
self.cookies = {} self.cookies = {}
self.socks5proxy = socks5proxy
self.proxy_connector = None self.proxy_connector = None
self.socks5_proxy_url = socks5_proxy_url self.socks5_proxy_url = socks5_proxy_url
self.blocked_domains = set() self.blocked_domains = set()
@ -107,25 +107,25 @@ class HttpClient:
filtered = session.cookie_jar.filter_cookies(domain) filtered = session.cookie_jar.filter_cookies(domain)
return filtered return filtered
async def make_request(self, url, method, async def make_request(self, url, method='GET',
response_type=RESPONSE_TEXT, response_type=RESPONSE_TEXT,
params=None, params=None,
data=None, data=None,
jd=None, jd=None,
stream_func=None, stream_func=None,
headers=None, headers=None,
use_proxy=False, use_proxy=False
**kw): ):
async with aiohttp.ClientSession() as session: connector = None
if use_proxy:
connector = ProxyConnector.from_url(self.socks5_proxy_url)
async with aiohttp.ClientSession(connector=connector) as session:
if params == {}: if params == {}:
params = None params = None
if data == {}: if data == {}:
data = None data = None
if jd == {}: if jd == {}:
jd = None jd = None
proxy = None
if use_proxy and self.socks5_proxy_url:
proxy = self.socks5_proxy_url
if headers == {}: if headers == {}:
headers = None headers = None
@ -134,15 +134,14 @@ class HttpClient:
params=params, params=params,
data=data, data=data,
json=jd, json=jd,
proxy=proxy, headers=headers)
headers=headers, **kw)
if resp.status==200: if resp.status==200:
return await self.handleResp(url, resp, response_type, stream_func=stream_func) return await self.handleResp(url, resp, response_type, stream_func=stream_func)
msg = f'http error({resp.status}, {url=},{params=}, {data=}, {jd=}, {headers=}, {kw=})' msg = f'http error({resp.status}, {url=},{params=}, {data=}, {jd=}, {headers=}, {kw=})'
exception(msg) exception(msg)
raise HttpError(resp.status, msg) raise HttpError(resp.status, msg)
async def request(self, url, method, async def request(self, url, method='GET',
response_type=RESPONSE_TEXT, response_type=RESPONSE_TEXT,
params=None, params=None,
data=None, data=None,
@ -152,41 +151,42 @@ class HttpClient:
**kw **kw
): ):
if self.socks5_proxy_url is None: if self.socks5_proxy_url is None:
resp = await self.make_request(url, method, resp = await self.make_request(url, method=method,
response_type=response_type, response_type=response_type,
params=params, params=params,
data=data, data=data,
jd=jd, jd=jd,
proxy=False, use_proxy=False,
stream_func=stream_func, stream_func=stream_func,
headers=headers, headers=headers
**kw) )
return resp return resp
domain = get_domain(url) domain = get_domain(url)
if domain not in self.blocked_domains: if domain not in self.blocked_domains:
try: try:
resp = await self.make_request(url, method, resp = await self.make_request(url, method=method,
response_type=response_type, response_type=response_type,
params=params, params=params,
data=data, data=data,
jd=jd, jd=jd,
proxy=False, use_proxy=False,
stream_func=stream_func, stream_func=stream_func,
headers=headers, headers=headers
**kw) )
return resp return resp
except aiohttp.ClientError: except:
if domain not in self.blocked_domains: if domain not in self.blocked_domains:
self.blocked_domains.add(domain)
self.save_cache() self.save_cache()
resp = await self.make_request(url, method, resp = await self.make_request(url, method=method,
response_type=response_type, response_type=response_type,
params=params, params=params,
data=data, data=data,
jd=jd, jd=jd,
proxy=True, use_proxy=True,
stream_func=stream_func, stream_func=stream_func,
headers=headers, headers=headers
**kw) )
return resp return resp
async def get(self,url,**kw): async def get(self,url,**kw):
@ -197,10 +197,10 @@ class HttpClient:
session = self.getsession(url) session = self.getsession(url)
class JsonHttpAPI: class JsonHttpAPI:
def __init__(self, env={}): def __init__(self, env={}, socks5_proxy_url=None):
self.env = env self.env = env
self.te = MyTemplateEngine([], env=env) self.te = MyTemplateEngine([], env=env)
self.hc = HttpClient() self.hc = HttpClient(socks5_proxy_url=socks5_proxy_url)
async def call(self, url, method='GET', ns={}, async def call(self, url, method='GET', ns={},
stream_func=None, stream_func=None,
@ -234,11 +234,13 @@ class JsonHttpAPI:
return ret return ret
if __name__ == '__main__': if __name__ == '__main__':
async def gbaidu(hc): async def main():
r = await hc.get('https://www.baidu.com') hc = HttpClient(socks5_proxy_url='socks5://localhost:1086')
r = await hc.request('https://www.baidu.com')
print(r)
r = await hc.request('https://www.google.com')
print(r) print(r)
await hc.close() await hc.close()
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
hc = HttpClient() loop.run_until_complete(main())
loop.run_until_complete(gbaidu(hc))