Make method one, use IP138 database to query domain name or IP address corresponding geographical location.
#-*-coding:gbk-*-
import urllib2
import re
try: While
True:
ipaddr = Raw_input ("Enter IP Or Domain Name: ")
if ipaddr = =" "" or ipaddr = = ' exit ': Break
else:
url = "http://www.ip138.com/ips138.asp?ip=%s& amp;action=2 "% ipaddr
u = urllib2.urlopen (URL)
s = u.read ()
#Get IP address
ip = re.findall (R ' \d{ 1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} ', s)
print ' \n****** Below result from IP138 Database * * * "
print" IP address : ", ip[0]
#Get IP address Location result
= Re.findall (R ' (<li>.*?</li>) ', s) for I-result
:
print i[4:-5]
print "*" *45
print "\ n"
except:
print "Not Data find"
Method Two, originally wanted to invoke Ali IP interface query IP attribution. It turns out that Ali's interface is very strong, mainly inaccurate, but free and has regional and ISP information. Here is the implementation code
#-*-Coding:utf-8-*-
Import Requests
def checkip (IP):
URL = ' http://ip.taobao.com/service/getIpInfo.php '
try:
r = Requests.get (URL, Params=ip, timeout=3)
except requests. Requestexception as E:
print (E)
else:
json_data = R.json ()
if Json_data[u ' code '] = = 0:
print ' Host Country: ' + json_data[u ' data '][u ' Country '].encode (' utf-8 ')
print ' location: ' + json_data[u ' data '][u ' area '].encode (' Utf-8 ')
print ' Province: ' + json_data[u ' data '][u ' region '].encode (' utf-8 ')
print ' City: ' + json_data[u ' data ' [ U ' city '].encode (' utf-8 ')
print ' operator: ' + json_data[u ' data '][u ' ISP '].encode (' utf-8 ')
else:
print ' Query failed, please try again later! '
ip={' ip ': ' 202.102.193.68 '}
checkip (IP)
However, multiple queries found that IP ownership is not accurate, so the use of 17mon IP query interface. But the 17mon fee and Free Library interface, I used the free test, the interface returned a limited number of fields, only countries, provinces, cities. The code is as follows
#-*-Coding:utf-8-*-
Import Requests
def lookup (IP):
URL = ' http://freeipapi.17mon.cn/' + IP
try:
r = Requests.get (URL, timeout=3)
except requests. Requestexception as E:
print (e)
json_data = R.json ()
print ' Country: ' + json_data[0].encode (' utf-8 ')
print ' Province: ' + json_data[1].encode (' utf-8 ')
print ' City: ' + json_data[2].encode (' Utf-8 ') return
(IP)
ip= ' 202.104.15.102 '
lookup (IP)
Testing is also good, the company to use or choose to buy a paid library query interface bar.
This part from the "Old Xu's private Dishes" blog, reprinted please contact the author!
The above mentioned is the entire content of this article, I hope you can enjoy.