add error handler aiohttp
This commit is contained in:
parent
c8d51d2e3d
commit
1a3a99ec72
27
stream.py
27
stream.py
@ -43,9 +43,14 @@ class ProxyElem():
|
||||
def __repr__(self):
|
||||
return str(self.proxy)
|
||||
async def content_type(self, url):
|
||||
ctype = "binary/octet-stream"
|
||||
try:
|
||||
async with self.session() as session:
|
||||
resp = await session.head(url)
|
||||
return resp.headers.get("Content-Type", "binary/octet-stream")
|
||||
ctype = resp.headers.get("Content-Type", "binary/octet-stream")
|
||||
except Exception as e:
|
||||
logger.info(e)
|
||||
return ctype
|
||||
async def proxy_url(self, current, path):
|
||||
data = {}
|
||||
data_list = [data]
|
||||
@ -61,11 +66,16 @@ class ProxyElem():
|
||||
data["proxy"] = self.proxy
|
||||
if proxy_server is None:
|
||||
return data["upstream"]
|
||||
|
||||
jdata = None
|
||||
try:
|
||||
async with self.session() as session:
|
||||
resp = await session.post(proxy_server, json=data_list)
|
||||
text = await resp.text()
|
||||
jdata = json.loads(text)
|
||||
logger.info(jdata)
|
||||
except Exception as e:
|
||||
logger.info(e)
|
||||
|
||||
if isinstance(jdata, list) and len(jdata) == 1:
|
||||
return jdata[0]
|
||||
else:
|
||||
@ -149,7 +159,10 @@ class UpstreamHandler():
|
||||
proxies[self.provider] = current_list
|
||||
break
|
||||
for delay in delays:
|
||||
try:
|
||||
await delay.session.close()
|
||||
except Exception as e:
|
||||
logger.info(e)
|
||||
async def meta(self):
|
||||
data = []
|
||||
try:
|
||||
@ -221,10 +234,14 @@ except Exception as e:
|
||||
logger.info(e)
|
||||
|
||||
async def rewrite(current, provider, proxy):
|
||||
ndata = None
|
||||
text = None
|
||||
try:
|
||||
async with proxy.session() as session:
|
||||
resp = await session.get(current)
|
||||
text = await resp.text()
|
||||
ndata = None
|
||||
except Exception as e:
|
||||
logger.info(e)
|
||||
if text is not None:
|
||||
links = []
|
||||
for line in resp.text.splitlines():
|
||||
@ -246,9 +263,13 @@ async def rewrite(current, provider, proxy):
|
||||
links.append(ldata)
|
||||
if isinstance(proxy_server, str):
|
||||
ndata = ""
|
||||
try:
|
||||
async with proxy.session() as session:
|
||||
resp = await session.post(proxy_server, json=links)
|
||||
link_text = await resp.text()
|
||||
except Exception as e:
|
||||
logger.info(e)
|
||||
else:
|
||||
if isinstance(link_text, str):
|
||||
links = json.loads(link_text)
|
||||
for line in text.splitlines():
|
||||
|
Loading…
Reference in New Issue
Block a user