gethostbyname
函数
- 功能:获取url 的域名的 IP,而不是整个URL的 IP
from socket import gethostbyname
url = "https://www.crifan.com/python_re_sub_detailed_introduction/"
print gethostbyname('www.crifan.com')
print gethostbyname('www.baidu.com')
def dns_check(url):
'''
探测URL的IP
'''
if url.find("//") != -1:
print url
print url.find("//")
url = url[url.find("//") + 2:]
print url
if url[-1] == '/':
url = url[:-1]
print url
try:
ip = gethostbyname(url)
except Exception,e:
print e
ip = ''
return ip
print dns_check(url)
107.151.176.131
61.135.169.125
https://www.crifan.com/python_re_sub_detailed_introduction/
6
www.crifan.com/python_re_sub_detailed_introduction/
www.crifan.com/python_re_sub_detailed_introduction
[Errno 11001] getaddrinfo failed
from urlparse import urlparse
url = "https://www.crifan.com/python_re_sub_detailed_introduction/"
parts = urlparse(url)
host = parts.netloc
print host
print gethostbyname(host)
www.crifan.com
107.151.176.131
hasattr()
, getattr()
, setattr()
函数
hasattr(object, name)
: 判断一个对象里面是否有 name 属性或者 name方法,返回BOOL值,有name特性返回True, 否则返回False。getattr(object,name)
: 获取对象object的属性或者方法,如果存在打印出来,如果不存在,打印出默认值,默认值可选。setattr(object,name)
:给对象的属性赋值,若属性不存在,先创建再赋值。
class test():
name="xiaohua"
def run(self):
return "HelloWord"
t = test()
print hasattr(t,"name")
print hasattr(t,"run")
print getattr(t,'name')
print getattr(t,'run')
if not hasattr(t,'age'):
setattr(t,'age',18)
print getattr(t,'age')
True
True
xiaohua
<bound method test.run of <__main__.test instance at 0x0000000002A64888>>
18