**18.2 使用asyncio 和 aiohttp包下载**
书中示例代码flags_asyncio.py,直接运行时会出现三个问题
1、控制台报错requests.exceptions.ProxyError: HTTPSConnectionPool
解决该问题的方法是参考https://zhuanlan.zhihu.com/p/350015032
1)将代理服务器关闭
2)在原报错环境中使用下面命令重装低版本 urllib3:
pip install urllib3==1.25.11
问题根源是urllib3 要支持 https 代理了,那么既然配置代理是 https 就尝试用 https 的方式去连接,但是由于代理服务器其实只支持 http,所以没法处理请求,ssl 握手阶段就出错了
2、报错Task exception was never retrieved
future: exception=TypeError(“‘_SessionRequestContextManager’ object is not iterable”,)>
参考https://blog.csdn.net/weixin_39646831/article/details/110787598
解决方法:将原代码中的
resp = yield from aiohttp.request('GET', url)
修改为
resp = yield from aiohttp.ClientSession().get(url)
import asyncio
import aiohttp
from flags import BASE_URL, save_flag, show, main
@asyncio.coroutine
def get_flag(cc):
url = '{}/{cc}/{cc}.gif'.format(BASE_URL, cc=cc.lower())
resp = yield from aiohttp.ClientSession().get(url)
image = yield from resp.read()
return image
@asyncio.coroutine