gpt4 book ai didi

python - 使用IP地址获取主机名-Python

转载 作者:行者123 更新时间:2023-12-03 11:55:40 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Python lookup hostname from IP with 1 second timeout

(2 个回答)


7年前关闭。




我正在尝试使用 ip 地址显示所有连接的机器名称,我可以通过检查连接来获取 IP 地址

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((addr,80))

我试过使用 s.getsockname , socket.gethostnames.getpeername这些都返回了相似的结果

如果我需要显示名称,我应该怎么做?例如
192.168.1.1     - 192.168.1.1
192.168.1.50 - 192.168.1.50
192.168.1.113 - 192.168.1.113
192.168.1.114 - 192.168.1.114
192.168.1.139 - 192.168.1.139

我需要像这样显示
192.168.1.1     - tom123
192.168.1.50 - allec
192.168.1.113 - john-pc
192.168.1.114 - bob
192.168.1.139 - anny

我试过 socket.gethostbyaddr("196.168.1.114") - 它给了我一个异常(exception)说

print socket.gethostbyaddr("196.168.1.114")

socket.herror: [Errno 1] Unknown host

最佳答案

我不知道这是否有帮助,但是 socket.getfqdn(IP_ADDRESS)返回主机名。

关于python - 使用IP地址获取主机名-Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27666625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com