First, preface
Because sometimes in the work to the domain name resolution and obtain the relevant IP attribution information, so write the script, convenient for their own query use.
Ii. contents of the script
#!/usr/bin/env python#coding:utf-8import dns.resolverimport urllibimport chardetimport sysimport reip_list = []query_domain = raw_input (' Please input a domain: ') # first query whether there is a CNAME, if any, get to a record through CNAME, if not directly get a record, and then return to a list of def get_iplist (domain): try: cn = dns.resolver.query ( Domain, ' CNAME ') for i in cn.response.answer: for j in i.items: cname_domain = j.to_text () print ' =========================== ============= ' print ' CNAME ---> ', cname_domain print ' ======================================== ' except: pass Else: a = dns.resolver.query (Cname_domain, ' A ') for i in A.response.answer: for j in i.items: try: ip_list.append (j.address) except: get_iplist (str (j)) &NBsp; try: a = dns.resolver.query (Domain, ' A ') for i in A.response.answer: for j in i.items: ip_list.append (j.address) except: pass return ip_list# Get IP attribution information by calling Ip138.com def get_ Ipbelong (domain_ip): url = ' http://ip138.com/ips138.asp?ip=%s&action=2 ' % domain_ip content = urllib.urlopen (URL). Read () typeencode = sys.getfilesystemencoding () infoencode = Chardet.detect (content). Get (' ENcoding ', ' Utf-8 ') html = content.decode (infoencode, ' ignore '). Encode ( Typeencode) ip = re.findall (R ' \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} ', html) result = re.findall (R ' (<li>.*?</li>) ', HTML) belong = result[0][14:-5] print '%-*s--->%*s ' % (18,ip[0], 20,belong) if __name__ == ' __main__ ': get_iplist (query_domain) for ip in ip_list: get_ Ipbelong (IP)
Third, the effect
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M02/84/54/wKiom1eNn4nRaLF6AAATTGNhCCE573.png "title=" effect "alt = "Wkiom1enn4nralf6aaattgnhcce573.png"/>
Iv. Related Information
Http://www.dnspython.org/examples.html
Http://www.cnblogs.com/CoolRandy/p/3251733.html
This article is from the "Gzguitar" blog, make sure to keep this source http://gzguitar.blog.51cto.com/5406286/1827705
Using Python script to implement domain name resolution and attribution information query