python - 带有 http 代理的 aiohttp https 请求失败
问题描述
我正在构建一个测试项目来学习 asyncio。我正在构建一个通过代理服务器获取多个页面的程序。
它适用于 http 页面,但适用于 https 页面。当我使用常规请求库时,我也可以使用 https 页面,但不能使用 asyncio。
我隔离了中断的代码:
import aiohttp
import asyncio
async def fetch(url):
async with aiohttp.ClientSession() as session:
async with session.get(url, proxy="http://192.168.0.2:9001") as response:
print(await response.text())
loop = asyncio.get_event_loop()
loop.run_until_complete(fetch("https://google.com")) #http websites do work
错误:
Traceback (most recent call last):
File "C:\Users\GG\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aiohttp\connector.py", line 936, in _wrap_create_connection
return await self._loop.create_connection(*args, **kwargs) # type: ignore # noqa
File "C:\Users\GG\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 1050, in create_connection
transport, protocol = await self._create_connection_transport(
File "C:\Users\GG\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 1080, in _create_connection_transport
await waiter
File "C:\Users\GG\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 395, in _loop_writing
self._write_fut = self._loop._proactor.send(self._sock, data)
File "C:\Users\GG\AppData\Local\Programs\Python\Python38-32\lib\asyncio\windows_events.py", line 525, in send
self._register_with_iocp(conn)
File "C:\Users\GG\AppData\Local\Programs\Python\Python38-32\lib\asyncio\windows_events.py", line 714, in _register_with_iocp
_overlapped.CreateIoCompletionPort(obj.fileno(), self._iocp, 0, 0)
OSError: [WinError 87] The parameter is incorrect
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/data/Python/playground/sayncio_session.py", line 10, in <module>
loop.run_until_complete(fetch("https://google.com")) #http websites do work
File "C:\Users\GG\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "C:/data/Python/playground/sayncio_session.py", line 6, in fetch
async with session.get(url, proxy="http://192.168.0.2:9001") as response:
File "C:\Users\GG\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aiohttp\client.py", line 1012, in __aenter__
self._resp = await self._coro
File "C:\Users\GG\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aiohttp\client.py", line 480, in _request
conn = await self._connector.connect(
File "C:\Users\GG\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aiohttp\connector.py", line 523, in connect
proto = await self._create_connection(req, traces, timeout)
File "C:\Users\GG\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aiohttp\connector.py", line 855, in _create_connection
_, proto = await self._create_proxy_connection(
File "C:\Users\GG\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aiohttp\connector.py", line 1093, in _create_proxy_connection
transport, proto = await self._wrap_create_connection(
File "C:\Users\GG\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aiohttp\connector.py", line 943, in _wrap_create_connection
raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host google.com:443 ssl:default [The parameter is incorrect]
Process finished with exit code 1
我正在使用 aiohttp=3.6.2 和 python=3.8.2
我该如何解决这个错误?
解决方案
在aiohttp
尝试使用ProactorEventLoop
. 从 Python 3.8 开始,它现在是 Windows 上的默认事件循环(而不是SelectorEventLoop
),因此在库得到修复之前,解决方法是显式切换回SelectorEventLoop
:
import aiohttp
import asyncio
async def fetch(url):
...
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
loop = asyncio.get_event_loop()
loop.run_until_complete(fetch(...))
相关:的跟踪器中的问题 #2245 。aiohttp
推荐阅读
- javascript - How can I replace the user prompt which currently determines the variable's value with an input box?
- dart - Widget is being rebuilt when Child Widget is calling a Push Named
- java - 春季启动 RequestBody 不起作用
- javascript - 没有调用函数,javascript向对象添加属性
- linux - wait_event_interruptible 和 wake_up_interruptible 需要 20 毫秒到 30 毫秒
- python - Efficiently grouping sets based on number of similar items
- javascript - 在 JSON 响应中将字符串 true/false 转换为布尔值
- python - Python Search and Replace new lines
- python - pyserial 使用 serial.tools.list_ports.grep() 找不到设备
- html - Do normal elements contain custom elements?