首页 > 解决方案 > 异步套接字,本地主机与本地 IP

问题描述

我正在编写一个 Asyncio 脚本。一切正常,但我对我在元组 asyncio 返回的地址中看到的内容有疑问。

这行代码根据我使用 localhost 或本地 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 时它们不在那里?

标签: pythonsocketsserverclientpython-asyncio

解决方案


其他字段似乎与IPv6 地址范围有关。

另请参见socket.getnameinfosocket.getpeername


推荐阅读