如何将asyncio create_connection用于IPv6地址?

时间:2018-08-09 15:34:08

标签: python python-3.x python-2.7 python-asyncio

在我的代码中,我尝试使用asyncio create_connection连接以使客户端连接到TCP服务器。由于某些原因,当我尝试使用IPv4地址时,连接成功:

event_loop.create_connection(client_factory, host='127.0.0.1', port=8190)

但是当我使用IPv6地址时连接失败:

event_loop.create_connection(client_factory, host='::1', port=8190)

使用IPv6环回地址时,出现OSError:

Traceback (most recent call last):
  File "C:\Users\<removed>\AppData\Local\Programs\Python\Python36\lib\asyncio\base_events.py", line 778, in create_connection
    raise exceptions[0]
  File "C:\Users\<removed>\AppData\Local\Programs\Python\Python36\lib\asyncio\base_events.py", line 765, in create_connection
    yield from self.sock_connect(sock, address)
  File "C:\Users\<removed>\AppData\Local\Programs\Python\Python36\lib\asyncio\proactor_events.py", line 451, in sock_connect
    return self._proactor.connect(sock, address)
  File "C:\Users\<removed>\AppData\Local\Programs\Python\Python36\lib\asyncio\windows_events.py", line 511, in connect
    ov.ConnectEx(conn.fileno(), address)
OSError: [WinError 10022] An invalid argument was supplied

查看文档,看起来create_connection的family参数默认为socket.AF_UNSPEC。我还通过指定家庭尝试了相同的通话:

event_loop.create_connection(client_factory, host='::1', port=8190, family=socket.AF_INET6)

但是我得到相同的结果。我知道将主机设置为'localhost'可以在此示例中使用,但是我想知道如何为create_connection使用任何IPv6地址。

0 个答案:

没有答案