Asyncio 套接字,本地主机与本地 IP

Asyncio Sockets, Localhost vs local IP

我正在编写一个 Asyncio 脚本。一切正常,但我对我在元组异步 returns 中看到的地址有疑问。

这行代码 returns 根据天气的不同,我使用本地主机或我的本地 IP 地址连接客户端。

(Server code)
addr = writer.get_extra_info('peername')
print("Received %r from %r" % (message, addr))

在我的客户端中使用 localhost 作为我的连接,我在我的服务器上看到了这个

(Client code)
reader, writer = await asyncio.open_connection('localhost', 8888, loop=asyncloop)

(Server prints)
Received 'Hello World!' from ('::1', 50402, 0, 0)

并在我的客户端中使用 IP 地址作为我的连接,

(Client code)
reader, writer = await asyncio.open_connection('192.168.147.200', 8888, loop=asyncloop)

(Server prints)
Received 'Hello World!' from ('192.168.147.139', 50313)

第一个元组中的两个零是什么意思?为什么当我连接到 IP 时它们不存在?

其他字段似乎与 IPv6 address scoping 相关。

另见 socket.getnameinfo and socket.getpeername

https://docs.python.org/3.5/library/socket.html?highlight=socket#socket-families

您的本地主机连接通过 IPv6 到达:

For AF_INET6 address family, a four-tuple (host, port, flowinfo, scopeid) is used, where flowinfo and scopeid represent the sin6_flowinfo and sin6_scope_id members in struct sockaddr_in6 in C. For socket module methods, flowinfo and scopeid can be omitted just for backward compatibility. Note, however, omission of scopeid can cause problems in manipulating scoped IPv6 addresses.

您的显式 IP 地址连接是 IPv4 连接,其中:

A pair (host, port) is used for the AF_INET address family, where host is a string representing either a hostname in Internet domain notation like 'daring.cwi.nl' or an IPv4 address like '100.50.200.5', and port is an integer.

主机名 'localhost' 可以解析为 IPv6 或 IPv4 地址,而 '192.168.147.200' 是一个明确的 IPv4 地址。