如何使用Python 3.x获取网站的IP地址? [英] How do I get a website's IP address using Python 3.x?

查看:149
本文介绍了如何使用Python 3.x获取网站的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表示域名的字符串。如何使用Python 3.x获取相应的IP地址?这样的一个例子:

 >>> get_ip('http://www.stackoverflow.com')
'64 .34.119.12'


解决方案

 >>> import socket 

>>> def get_ips_for_host(host):
try:
ips = socket.gethostbyname_ex(host)
except socket.gaierror:
ips = []
return ips

>>> ips = get_ips_for_host('www.google.com')
>>> print(repr(ips))
('www.l.google.com',[],['74 .125.77.104','74 .125.77.147','74 .125.77.99'])


I have a string representing a domain name. How can I get the corresponding IP address using Python 3.x? Something like this:

>>> get_ip('http://www.stackoverflow.com')
'64.34.119.12'

解决方案

>>> import socket

>>> def get_ips_for_host(host):
        try:
            ips = socket.gethostbyname_ex(host)
        except socket.gaierror:
            ips=[]
        return ips

>>> ips = get_ips_for_host('www.google.com')
>>> print(repr(ips))
('www.l.google.com', [], ['74.125.77.104', '74.125.77.147', '74.125.77.99'])

这篇关于如何使用Python 3.x获取网站的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆