Reputation: 2123
There's probably an easy way to do this with some API, but web programming is not my forte. How can I figure out the user's country in Python? I need to use an OS-agnostic solution, and I'd prefer to only have to use the standard library. Is there something I can do with IP addresses, maybe?
Upvotes: 0
Views: 4711
Reputation: 71
How can I figure out the user's country in Python?
Getting a user's country information from their IP address is pretty straightforward with IPinfo.
I need to use an OS-agnostic solution, and I'd prefer to only have to use the standard library.
Even though it is far easier to use IPinfo with Python Module or calling API via the requests library, you can still get the IP geolocation data through the standard libraries. You have to use the urllib.request
and json
library for this purpose, which comes standard with Python installation.
Follow the instructions:
# no need to pip install anything
from urllib.request import urlopen # to make the request to the API
import json # to parse the API response
# declare your IP Address here
ip_address = "8.8.8.8"
# get your token from IPinfo's account dashboard
token = ""
# create the url for the API, using f-string
url = f"https://www.ipinfo.io/{ip_address}?token={token}"
# call the API and save the response
with urlopen(url) as response:
response_content = response.read()
# parsing the response
data = json.loads(response_content)
print(data)
This data
here is a Python Dictionary object, that looks like this:
{'anycast': True,
'city': 'Mountain View',
'country': 'US',
'hostname': 'dns.google',
'ip': '8.8.8.8',
'loc': '37.4056,-122.0775',
'org': 'AS15169 Google LLC',
'postal': '94043',
'region': 'California',
'timezone': 'America/Los_Angeles'}
Now you can get any value from the data
variable, like so:
print(data['country']) # Output: 'US'
print(data['city']) # Output: 'Mountain View'
# loc stands for location.
print(data['loc']) # Output: '37.4056,-122.0775'
Upvotes: 3
Reputation: 1522
If you know the public IP of the user then you can do so by using freegeip. Below is the Python 3.4.2 code for getting location and time zone.
>>> import requests
>>> ip = '141.70.111.66'
>>> url = 'http://freegeoip.net/json/'+ip
>>> r = requests.get(url)
>>> js = r.json()
>>> js['country_code']
'DE'
>>> js['country_name']
'Germany'
>>> js['time_zone']
'Europe/Berlin'
>>> js['city']
'Stuttgart'
>>> js.items()
dict_items([('latitude', 48.7667), ('ip', '141.70.111.66'), ('region_code', 'BW'), ('country_code', 'DE'), ('city', 'Stuttgart'), ('zip_code', '70173'), ('country_name', 'Germany'), ('longitude', 9.1833), ('region_name', 'Baden-Württemberg Region'), ('time_zone', 'Europe/Berlin'), ('metro_code', 0)])
Upvotes: 0
Reputation: 2123
Joran Beasely gave a good answer here, but since it came in the form of a comment rather than a reply I can't mark it as my accepted answer. Still, thanks for the help!
Upvotes: 0
Reputation: 612
You could use geo-ip for Python: http://code.google.com/p/python-geoip/ It's default free database is good enough to get users' country with good accuracy.
Upvotes: 1