Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems when using proxy under Windows #4536

Closed
libra146 opened this issue Jan 28, 2020 · 11 comments
Closed

Problems when using proxy under Windows #4536

libra146 opened this issue Jan 28, 2020 · 11 comments

Comments

@libra146
Copy link

libra146 commented Jan 28, 2020

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

@webknjaz
Copy link
Member

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.

@libra146
Copy link
Author

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

@hh-h
Copy link
Contributor

hh-h commented Jan 29, 2020

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.

@libra146
Copy link
Author

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.

@hh-h
Copy link
Contributor

hh-h commented Jan 29, 2020

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)

@libra146
Copy link
Author

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.

@Canyun
Copy link

Canyun commented Jul 3, 2020

#4536 (comment)

it worked for me, thanks !

@lisonge
Copy link

lisonge commented Sep 24, 2020

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

@etiennebeaulac
Copy link

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

@libra146 libra146 closed this as completed Dec 5, 2020
@darkcike
Copy link

darkcike commented Jan 4, 2021

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)

@sap2me
Copy link

sap2me commented Jan 18, 2021

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

@aio-libs aio-libs locked as off-topic and limited conversation to collaborators Jan 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants