Python stunt-associating IP addresses and physical locations with Pygeoip

Source: Internet
Author: User
Tags geoip

Preparatory work

To associate IP with a physical location, we need to have a database that contains such a corresponding relationship.

We can use the open source database geolitecity, which can more accurately associate the IP address with the city in which it resides.

: http://dev.maxmind.com/geoip/legacy/geolite/

After download we unzip: xz-d GeoLiteCity.dat.xz, such as:/my/lib/ip/geolitecity.dat

Install the Pygeoip library. This library is used to query the Geolitecity database

Code:

#!/usr/bin/python#--*--coding=utf-8--*--import PYGEOIPGI = Pygeoip. GeoIP ('/my/lib/ip/geolitecity.dat ') def Printrecord (TGT): rec = gi.record_by_addr (TGT) city = rec[' city ']region = rec[' Region_code ']country = rec[' country_name ']long = rec[' longitude ']lat = rec[' latitude ']print ' [*] Host: ' + TGT + ' Geo-locat ed. ' Print ' [+] ' + str (city) + ', ' +str (region) + ', ' +str (country) print ' [+] Longitude: ' +str (LAT) + ', Dimension: ' + str (long) TGT = ' 183. 141.110.74 ' Printrecord (TGT)

183.141.110.74 is a random proxy IP address, check the address:

Query results

Python stunt-associating IP addresses and physical locations with Pygeoip

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.