#!/usr/bin/env python3.5
import asyncio
import aiohttp
url = "http://eniig.dk"
async def main():
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
try:
body = await response.read()
print(response.method)
print(body)
except UnicodeDecodeError as msg:
print(msg)
print(body)
except Exception as msg:
print(msg)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
这段代码报错:
400, message='400, message='无效常量字符串''
如果 URL 指定“https”,则一切正常。但是,如果事先不知道协议(protocol),如何避免错误呢?
什么替代方案会建议 aiohttp?
更新: 我已经很久没有来过这里了。忘了说代码是在 Debian 8 stable 下运行的。通过将 ClientSession() 和 get() 方法替换为 TCPConnector() 和 request() 解决了这个问题
最佳答案
代码看起来不错。
使用此版本的 aiohttp
为我工作:
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
关于python - aiohttp 错误无效常量字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43306148/