How to detect the country and city of a user accessing your site?

Question:

How can you detect the country of origin of the users accessing your site?

I’m using Google Analytics on my site and can see that I have users coming from different regions of the world.

But within my application I would like to provide some additional customization based on the country and perhaps the city.

Is it possible to detect this infomation from the browser? This is a Python web application.

Asked By: Don King

||

Answers:

Get the visitor’s IP and check it with a geolocation web service (or purchase a geolocation database if you’re keen to host that determination in-house).

Answered By: Alex Martelli

Grab and gunzip http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz, install the GeoIP-Python package (python-geoip package if you’re in Debian or Ubuntu, otherwise install http://geolite.maxmind.com/download/geoip/api/python/GeoIP-Python-1.2.4.tar.gz), and do:

import GeoIP
gi = GeoIP.open("GeoLiteCity.dat", GeoIP.GEOIP_INDEX_CACHE | GeoIP.GEOIP_CHECK_CACHE)
print gi.record_by_name("74.125.67.100") # a www.google.com IP

{'city': 'Mountain View', 'region_name': 'California', 'region': 'CA', 'area_code': 650, 'time_zone': 'America/Los_Angeles', 'longitude': -122.05740356445312, 'country_code3': 'USA', 'latitude': 37.419200897216797, 'postal_code': '94043', 'dma_code': 807, 'country_code': 'US', 'country_name': 'United States'}

The database is free (http://geolite.maxmind.com/download/geoip/database/LICENSE.txt). They do sell it, too; I think that just gets you more frequent updates.

Answered By: Glenn Maynard

Max mind costs money.
For a free yet country code only solution you can try:
https://pypi.python.org/pypi/geoip2nation/

pip install geoip2nation

from geoip import geoip
r = geoip.GeoIp()
r.load_memory()
r.resolve("12.12.12.12").country_code
#This prints : 'US'

print r.resolve("123.44.57.4")
#This prints : {'country': 'Korea (South)', 'host_name': '', 'country_code': 'KR'}

r.resolve2("12.12.12.12")
#This prints : 'US'
Answered By: Mitzi

How can you detect the country of origin of the users accessing your site?

I would like to provide some additional customization based on the country and perhaps the city.

Is it possible to detect this infomation from the browser?

If you want to get the country and city information of users accessing your site, you have two options:

Option 1: Geolocation API ⇒ Precise but requires permission

The HTML Geolocation API will prompt the user for permission to provide their location.

This method is the most accurate way to get location of the user. But the catch is that they have to manually consent to providing their information.

You can get up to street level accuracy with HTML Geolocation API.

Option 2: Use a IP Geolocation Service ⇒ Get city/country information from visitors without asking

You can use a IP Geolocation service like our service IPinfo. You can get near 100% accuracy when it comes to identifying the city and country of the visitor just based on their IP address. When you pass the visitors IP address to IPinfo’s API you will get response with the visitors: city, region, country, postal code and other geolocaiton information. On the free tier you get 50,000 requests/month and it is pretty easy to use through our API or the Python library.

Using the Python library

First install the library with pip:

pip install ipinfo

Example code:

# import the ipinfo module after you have installed it
import ipinfo 

# after you have signed up, get your token from: https://ipinfo.io/account/token
access_token = 'insert_token_here'


# initialize the handler which you will use to make lookup
handler = ipinfo.getHandler(access_token)

# pass the IP address
ip_address = '104.193.114.247'


# get geolocation and other information from the IP address
details = handler.getDetails(ip_address)

print(details.city)
# Output: Seattle
print(details.region)
# Output: Washington
print(details.country)
# Output: US

# or you can print out a dictionary that contains all the details
print(details.all)
Answered By: Reincoder
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.