Python USES an online API to query for instances of geographic location information corresponding to an IP

  • 2020-04-02 13:41:14
  • OfStack

The content in this article comes from the initial stage of setting up my blog with VPS in the United States last year. There were a lot of malicious access. I made a lot of statistics based on the source IP in the access log, and I also used the source IP of the most visited malicious access to query its geographical location information. So, I've used something that looks up geographic location information based on IP, and now I'm sharing a little bit of that.

Some of the apis for querying location, operator, etc., based on IP (based on my limited experience) are as follows:
1. Taobao API (recommended) : http://ip.taobao.com/service/getIpInfo.php? IP = 110.84.0.129
2. Foreign freegeoip.net (recommended) : http://freegeoip.net/json/110.84.0.129 also provides the longitude and latitude information (but not probable)
3. Sina API:http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = json&ip = 110.84.0.129
http://ip.qq.com/cgi-bin/searchip? Searchip1 = 110.84.0.129
5. IP. Cn website: http://www.ip.cn/index.php? IP = 110.84.0.129
6. Ip-api.com: http://ip-api.com/json/110.84.0.129 (looks nice, seemingly returned directly in Chinese city information, document in ip-api.com/docs/api:json)
7. http://www.locatorhq.com/ip-to-location-api/documentation.php (this to register to use, but also used?)

(the second website of freegeoip.net and IP data generated, code in: https://github.com/fiorix/freegeoip).

Why are the 4th and 5th two web queries also recommended? This is due to the fact that the information they provide is accurate, and the fact that the page information is automatically fetched (probably using the PhantomJS I wrote about) and is easily written to the program as an API.

According to IP query geographical location information, I wrote it into a relatively general Python library (providing apis for the four query methods mentioned above, including 1, 2, 4 and 5), which can query the regional information and ISP information according to IP, the specific code is as follows:
https://github.com/smilejay/python/blob/master/py2013/iplocation.py
Note that webdriver and PhantomJS are used in the parsing of ip.cn pages.


#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Created on Oct 20, 2013
@summary: geography info about an IP address
@author: Jay <smile665@gmail.com> http://smilejay.com/
'''

import json, urllib2
import re
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

 
class location_freegeoip():
    '''
build the mapping of the ip address and its location.
the geo info is from <freegeoip.net>
'''

    def __init__(self, ip):
        '''
Constructor of location_freegeoip class
'''
        self.ip = ip
        self.api_format = 'json'
        self.api_url = 'http://freegeoip.net/%s/%s' % (self.api_format, self.ip)

    def get_geoinfo(self):
        """ get the geo info from the remote API.
return a dict about the location.
"""
        urlobj = urllib2.urlopen(self.api_url)
        data = urlobj.read()
        datadict = json.loads(data, encoding='utf-8')
# print datadict
        return datadict

    def get_country(self):
        key = 'country_name'
        datadict = self.get_geoinfo()
        return datadict[key]

    def get_region(self):
        key = 'region_name'
        datadict = self.get_geoinfo()
        return datadict[key]

    def get_city(self):
        key = 'city'
        datadict = self.get_geoinfo()
        return datadict[key]

class location_taobao():
    '''
build the mapping of the ip address and its location
the geo info is from Taobao
e.g. http://ip.taobao.com/service/getIpInfo.php?ip=112.111.184.63
The getIpInfo API from Taobao returns a JSON object.
'''
    def __init__(self, ip):
        self.ip = ip
        self.api_url = 'http://ip.taobao.com/service/getIpInfo.php?ip=%s' % self.ip

    def get_geoinfo(self):
        """ get the geo info from the remote API.
return a dict about the location.
"""
        urlobj = urllib2.urlopen(self.api_url)
        data = urlobj.read()
        datadict = json.loads(data, encoding='utf-8')
# print datadict
        return datadict['data']

    def get_country(self):
        key = u'country'
        datadict = self.get_geoinfo()
        return datadict[key]

    def get_region(self):
        key = 'region'
        datadict = self.get_geoinfo()
        return datadict[key]

    def get_city(self):
        key = 'city'
        datadict = self.get_geoinfo()
        return datadict[key]

    def get_isp(self):
        key = 'isp'
        datadict = self.get_geoinfo()
        return datadict[key]

 
class location_qq():
    '''
build the mapping of the ip address and its location.
the geo info is from Tencent.
Note: the content of the Tencent's API return page is encoded by 'gb2312'.
e.g. http://ip.qq.com/cgi-bin/searchip?searchip1=112.111.184.64
'''
    def __init__(self, ip):
        '''
Construction of location_ipdotcn class.
'''
        self.ip = ip
        self.api_url = 'http://ip.qq.com/cgi-bin/searchip?searchip1=%s' % ip

    def get_geoinfo(self):
        urlobj = urllib2.urlopen(self.api_url)
        data = urlobj.read().decode('gb2312').encode('utf8')
        pattern = re.compile(r' the IP Location: <span>(.+)</span>')
        m = re.search(pattern, data)
        if m != None:
            return m.group(1).split(' ')
        else:
            return None

    def get_region(self):
        return self.get_geoinfo()[0]

    def get_isp(self):
        return self.get_geoinfo()[1]

 
class location_ipdotcn():
    '''
build the mapping of the ip address and its location.
the geo info is from www.ip.cn
need to use PhantomJS to open the URL to render its JS
'''
    def __init__(self, ip):
        '''
Construction of location_ipdotcn class.
'''
        self.ip = ip
        self.api_url = 'http://www.ip.cn/%s' % ip

    def get_geoinfo(self):
        dcap = dict(DesiredCapabilities.PHANTOMJS)
        dcap["phantomjs.page.settings.userAgent"] = (
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/29.0 " )
        driver = webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs', desired_capabilities=dcap)
        driver.get(self.api_url)
        text = driver.find_element_by_xpath('//div[@id="result"]/div/p').text
        res = text.split(' From: ')[1].split(' ')
        driver.quit()
        return res

    def get_region(self):
        return self.get_geoinfo()[0]

    def get_isp(self):
        return self.get_geoinfo()[1]

 
if __name__ == '__main__':
    ip = '110.84.0.129'
# iploc = location_taobao(ip)
# print iploc.get_geoinfo()
# print iploc.get_country()
# print iploc.get_region()
# print iploc.get_city()
# print iploc.get_isp()
# iploc = location_qq(ip)
    iploc = location_ipdotcn(ip)
# iploc.get_geoinfo()
    print iploc.get_region()
    print iploc.get_isp()


Related articles: