简体   繁体   English

ping网站地址以返回IP

[英]Ping a website address to return an IP

Hey, i'm new to python programing and i have problem that i can't decifer the answer to though a google search. 嘿,我是python编程的新手,但我遇到的问题是我无法通过Google搜索来确定答案。

I need to ping a website address (eg www.google.com) and have python return me a IP address. 我需要ping一个网站地址(例如www.google.com),然后让python返回我的IP地址。

Cheers 干杯

you want to do a "DNS lookup" : 您想执行“ DNS查找”:

import socket
print socket.gethostbyname('www.google.com')

Output: 输出:

74.125.230.112

EDIT: 编辑:

For IPv6 support, you can use getaddrinfo (). 对于IPv6支持,可以使用getaddrinfo ()。 However, I'm not sure that is what you want ;) 但是,我不确定这就是您想要的;)

You want to do an NS lookup? 您想进行NS查找吗?

>>> import socket
>>> print socket.getaddrinfo('www.google.com', 80)

@Jarred, gethostbyname() as stated in the documentation , does not support IPv6. 文档中所述,@Jarred,gethostbyname()不支持IPv6。 Also if you look at the result of getaddrinfo() , there are other ip addresses related to "www.google.com". 另外,如果您查看getaddrinfo()的结果,则还有其他与“ www.google.com”相关的ip地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM