Python:如何从 FQDN 获取 IP 地址
如果我有一个 FQDN,例如 www.google.com,我如何获取相应的 IP 地址?
If I have an FQDN e.g., www.google.com, how do I get the corresponding IP address?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法是
socket.gethostbyname()
。The easiest way to do this is
socket.gethostbyname()
.您可以使用socket.getaddrinfo。这将为您提供与该名称相关的不同 IP 地址,并且还可以为您提供 IPv6 地址。
摘自文档:
注意:
gethostbyname
在 C 中已被弃用(Pythonsocket.gethostbyname
是用它实现的),因为它不支持 IPv6 地址,并且getaddrinfo< /code> 是推荐的替代品。
You can use
socket.getaddrinfo
. That will give you the differents IP address associated with the name, and can also give you the IPv6 address.From the documentation:
Note:
gethostbyname
is deprecated in C (and Pythonsocket.gethostbyname
is implemented with it) as it does not support IPv6 addresses, andgetaddrinfo
is the recommended replacement.使用
socket.gethostbyname(hostname)
请参阅:http: //docs.python.org/library/socket.html#socket.gethostbynameUse
socket.gethostbyname(hostname)
see: http://docs.python.org/library/socket.html#socket.gethostbyname