Aiohttp: Problems when using proxy under Windows

Created on 28 Jan 2020  路  8Comments  路  Source: aio-libs/aiohttp

Long story short

I get an error when running the following code under Python 3.8.1 and aiohttp3.6.2 under Windows

import aiohttp
import asyncio


async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get('https://python.org', proxy='http://127.0.0.1:8888') as response:
            print("Status:", response.status)
            print("Content-type:", response.headers['content-type'])

            html = await response.text()
            print("Body:", html[:15], "...")


loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Expected behaviour

Status: 200
Content-type: text/html; charset=utf-8
Body: <!doctype html> ...

Actual behaviour

Traceback (most recent call last):
  File "C:\Python38\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:\Python38\lib\asyncio\base_events.py", line 1046, in create_connection

    transport, protocol = await self._create_connection_transport(
  File "C:\Python38\lib\asyncio\base_events.py", line 1076, in _create_connectio
n_transport
    await waiter
  File "C:\Python38\lib\asyncio\proactor_events.py", line 395, in _loop_writing
    self._write_fut = self._loop._proactor.send(self._sock, data)
  File "C:\Python38\lib\asyncio\windows_events.py", line 525, in send
    self._register_with_iocp(conn)
  File "C:\Python38\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 "test5.py", line 16, in <module>
    loop.run_until_complete(main())
  File "C:\Python38\lib\asyncio\base_events.py", line 612, in run_until_complete

    return future.result()
  File "test5.py", line 7, in main
    async with session.get('https://python.org', proxy='http://192.168.233.1:108
8') as response:
  File "C:\Python38\lib\site-packages\aiohttp\client.py", line 1012, in __aenter
__
    self._resp = await self._coro
  File "C:\Python38\lib\site-packages\aiohttp\client.py", line 480, in _request
    conn = await self._connector.connect(
  File "C:\Python38\lib\site-packages\aiohttp\connector.py", line 523, in connec
t
    proto = await self._create_connection(req, traces, timeout)
  File "C:\Python38\lib\site-packages\aiohttp\connector.py", line 855, in _creat
e_connection
    _, proto = await self._create_proxy_connection(
  File "C:\Python38\lib\site-packages\aiohttp\connector.py", line 1093, in _crea
te_proxy_connection
    transport, proto = await self._wrap_create_connection(
  File "C:\Python38\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 python.or
g:443 ssl:default [The parameter is incorrect]

Steps to reproduce

Run the above code

Your environment

OS: windows7
proxy: v2ray
aiohttp: 3.6.2(client)
python: 3.8.1

When I remove the proxy parameter, it works fine

bug server

Most helpful comment

So you have to wait for aiohttp 3.7 release then. As a workaround either use Python 3.7 or use selector loop, not proactor on windows.

policy = asyncio.WindowsSelectorEventLoopPolicy()
asyncio.set_event_loop_policy(policy)

All 8 comments

g:443 ssl:default [鍙傛暟閿欒銆俔

I think you should rerun it with en_US locale so that we'd have the actual error message in English.

g:443 ssl:default [鍙傛暟閿欒銆俔

I think you should rerun it with en_US locale so that we'd have the actual error message in English.

Sorry i ignored it.
Now i have modified it

AFAIK it is related to changed default event loop for python 3.8 on Windows and/or start_tls(). Could you run your example with Python 3.7 by chance? 3.6.2 doesn't fully support Python 3.8.

AFAIK it is related to changed default event loop for python 3.8 on Windows and/or start_tls(). Could you run your example with Python 3.7 by chance? 3.6.2 doesn't fully support Python 3.8.

When I run it using Python3.7, it works fine.

So you have to wait for aiohttp 3.7 release then. As a workaround either use Python 3.7 or use selector loop, not proactor on windows.

policy = asyncio.WindowsSelectorEventLoopPolicy()
asyncio.set_event_loop_policy(policy)

So you have to wait for aiohttp 3.7 release then. As a workaround either use Python 3.7 or use selector loop, not proactor on windows.

policy = asyncio.WindowsSelectorEventLoopPolicy()
asyncio.set_event_loop_policy(policy)

OK, I will try it, thank you very much.You can close this issue now or wait for aiohttp3.7 release.

import sys
if sys.version_info[0] == 3 and sys.version_info[1] >= 8 and sys.platform.startswith('win'):
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

it also useful in some problem on httpx some issues

Was this page helpful?
0 / 5 - 0 ratings