🐛 Fix a bug by aiohttp (#149)

🐛 修复 aiohttp 下载文件的错误
This commit is contained in:
Xtao_dada 2021-11-25 20:11:02 +08:00 committed by GitHub
parent 849a0217b9
commit 8a1505d769
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 7 deletions

View File

@ -32,8 +32,8 @@ def remove_plugin(name):
async def download(name):
html = await get(f'{git_source}{name}.py')
with open(f'plugins/{name}.py', mode='wb') as f:
f.write(html.content)
with open(f'plugins/{name}.py', mode='w') as f:
f.write(html.text)
return f'plugins/{name}.py'

View File

@ -196,7 +196,7 @@ async def speedtest(context):
f"Timestamp: `{result['timestamp']}`"
)
# 开始处理图片
data = (await get(f"{result['result']['url']}.png")).content
data = (await get(f"{result['result']['url']}.png")).text
with open('speedtest.png', mode='wb') as f:
f.write(data)
try:
@ -273,7 +273,7 @@ async def speedtest(context):
f"Timestamp: `{result['timestamp']}`"
)
# 开始处理图片
data = (await get(result['share'])).content
data = (await get(result['share'])).text
with open('speedtest.png', mode='wb') as f:
f.write(data)
try:

View File

@ -25,10 +25,10 @@ class AiohttpResp:
"""
重写返回类型
"""
def __init__(self, text: str, content: bytes, status_code: int):
def __init__(self, text: Any, content: bytes, status_code: int):
"""
Args:
text (str): 网页内容
text (Any): 网页内容
content (bytes): 文件内容
status_code (int): 网页状态码
"""
@ -286,7 +286,10 @@ async def request(method: str,
session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=timeout))
resp = await session.request(**config_)
# 返回请求
resp_data = await resp.text()
try:
resp_data = await resp.text()
except UnicodeDecodeError:
resp_data = await resp.read()
content = await resp.content.read()
status_code = resp.status
await session.close()