#!/usr/bin/python#coding:utf-8import urllibimport reimport sysdef isip (s): return len ([I for i in s.split ('. ') if (0<= int (i) <= 255)]) == 4def url (IP): Uip=urllib.urlopen ('/HTTP/ wap.ip138.com/ip.asp?ip=%s '%ip) fip=uip.read () Rip=re.compile (r) <br/><b> query result: (. *) </b><br/ > ") result=rip.findall (FIP) print "%s\t %s " % (Ip,result[0]) def do (domain): url=urllib.urlopen (' http://wap.ip138.com/ip.asp?ip=%s '%domain) f=url.read () r=re.compile ( R ' > (. *) <br/><b> Query results: (. *) </b><br/> ') result=r.findall (f) #print type (Result) for i in result: print "%s\t %s\t %s\t" % (Domain,i[0],i[1]) if __name__ == "__main__": If len (SYS.ARGV) < 2:print "Please enter an IP address or domain name (example: 192.168.1.1 / www.baidu.com)" Sys.exit () input=sys.argv[1]if not Re.findall (' (\d{1,3}\.) {3}\d{1,3} ', INPUT): if re.findall (R ' (\w+\)? (\w+) (\.\d+) {()} ', INPUT) : domain=inputdo (DOMAIN) else: print "The input IP address and domain name format is wrong!" "Else: if isip (INPUT) : ipaddress=inputurl (IPADDRESS) else: print "ip address is not legal, please re-enter! "
Python Learning-use ip138 to query IP address attribution