aiohttp icon indicating copy to clipboard operation
aiohttp copied to clipboard

Problems when using proxy under Windows

Open libra146 opened this issue 6 years ago • 11 comments

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

libra146 avatar Jan 28 '20 08:01 libra146

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.

webknjaz avatar Jan 28 '20 16:01 webknjaz

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

libra146 avatar Jan 29 '20 03:01 libra146

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.

hh-h avatar Jan 29 '20 10:01 hh-h

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.

libra146 avatar Jan 29 '20 11:01 libra146

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)

hh-h avatar Jan 29 '20 12:01 hh-h

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.

libra146 avatar Jan 29 '20 12:01 libra146

https://github.com/aio-libs/aiohttp/issues/4536#issuecomment-579740877

it worked for me, thanks !

Canyun avatar Jul 03 '20 17:07 Canyun

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

lisonge avatar Sep 24 '20 16:09 lisonge

@webknjaz @hh-h Any update on when this will be fixed? I have encountered the same issue today.

etiennebeaulac avatar Nov 24 '20 21:11 etiennebeaulac

I see aiohttp 3.7.3 released and this problem still stays@hh-j

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)

darkcike avatar Jan 04 '21 10:01 darkcike

Same problem with Python 3.8.5 for windows. With ubuntu everything works fine.

sap2me avatar Jan 18 '21 17:01 sap2me