#!/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='invalid constant string''
如果URL指定“https”,则一切正常。但如果事先不知道协议,如何避免错误?
有哪些替代方案可以建议aiohttp?
UPD: 我已经很久没有来过这里了。忘了说代码是在Debian 8 stable下运行的。通过TCPConnector()和request()
替换ClientSession()和get()方法解决了这个问题。答案 0 :(得分:2)
它看起来像一个非常好的代码。
使用此版本的aiohttp
: