Python to query IP attribution

Source: Internet
Author: User

Originally wanted to call Ali's IP interface to query IP attribution. The results found that Ali's interface is not very powerful, mainly inaccurate, but is free of charge and has regional and ISP information. The following is the implementation code

# -*- coding: utf-8 -*-import requestsdef 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  ' Country:  '  + json_data[u ' data '][u ' country '].encode (' Utf-8 ')             print  ' area:  '  + 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   ' owned carrier: '  + 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)


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/54/9D/wKiom1SHvnSy5mXdAACUq8a9nqg911.jpg "title=" Ali.jpg "alt=" Wkiom1shvnsy5mxdaacuq8a9nqg911.jpg "/>

But multiple queries found that IP attribution is not accurate, so use 17mon IP query interface. But 17mon cent pay and free library interface, I use the free test, interface returned field is limited, only country, province, city. The code is as follows

# -*- coding: utf-8 -*-import requestsdef  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) 

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/54/9D/wKiom1SHv5Ch3WojAAB9iRfaqo0333.jpg "title=" I17.jpg "alt=" Wkiom1shv5ch3wojaab9irfaqo0333.jpg "/>

Testing is also good, the company to use or choose to buy a paid library query interface bar.

This article from "Lao Xu's Private Cuisine" blog, reproduced please contact the author!

Python to query IP attribution

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: info-contact@alibabacloud.com 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.