Python implementation query IP address location _python

Source: Internet
Author: User
Tags get ip

Make method one, use IP138 database to query domain name or IP address corresponding geographical location.

import urllib2
import re
try: While
  ipaddr = Raw_input ("Enter IP Or Domain Name: ")
  if ipaddr = =" "" or ipaddr = = ' exit ': Break
   url = " amp;action=2 "% ipaddr
   u = urllib2.urlopen (URL)
   s = ()
   #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"
 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

Import Requests
def checkip (IP):
  URL = ' '
    r = Requests.get (URL, Params=ip, timeout=3)
  except requests. Requestexception as E:
    print (E)
    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 ')
      print ' Query failed, please try again later! '
ip={' ip ': ' '}
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

Import Requests
def lookup (IP):
  URL = '' + IP
    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= ' '
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.