Find Country from IP address in Python
In this tutorial, we will learn how to find out the country name of the IP address that is given to us with Python programming.
First of all, let us understand what is an IP Address, so in simple words, an IP address is a label that is used to identify one or more devices on a computer network, such as the internet.
So for this program, we will be requiring API access from IPdata. You can also refer to any other site that provides access to such similar data as we will be discussing below. API access is nothing both accessing data from an API often using an API key (as in this case), data is received in the form of JSON files. A JSON file is basically a data structure format that allows storing objects as text. We don’t need much detail about it if you want to then you can search it on the web.
We will be needing an API key in order to access the data from this website. In order to get an API key, we need to sign up on that website and after signing up we will be receiving our API key through our mail.
Also, read: Getting the IP address of a URL in Python
Python program to get country name from IP Address
Let’s begin with the coding part.
First of all, we will need to import the necessary library.
import requests import os import json
In our program:
Requests library is for getting data from the given URL.
OS library is for accessing the environment variables.
JSON library is for manipulating with JSON files.
api_key = str(os.environ.get('api_key_country', '-1'))
Then I stored my API key into the variable api_key. As you can see I am using an environment variable for this purpose, the reason is the API key is an important key given to an individual that should not be disclosed publicly and that’s why it should not directly be shown in our code. So I first created an Environment variable api_key_country, assigned it the value of my API key and then accessed it using the OS library.
print("Welcome\n") print("IP Address to Country Name\n") ip_address = input("Enter the IP Address\n") print("Wait looking for the Country Name")
Then we will ask the user for the IP address and I have stored it in the ip_address variable.
data = requests.get(f'https://api.ipdata.co/{ip_address}?api-key={api_key}').json()
Then we will be getting the JSON file from the specific URL in the data variable.
To look up a specific IP Address we will be using the following format:
https://api.ipdata.co/8.8.8.8?api-key=test
where ‘8.8.8.8’ is the IP address of which we want to find the country name and test is our API key.
If you print the JSON file it will give you the required data but it won’t be much easy to read so to make it more readable we will first convert it into a Python string and then display it using proper indentation.
For converting it into a string we will be using dumps feature provided in the JSON library.
data_str = json.dumps(data, indent= 2)
Now data_str variable consists of the required string with proper indentation. We will print this to see what all data we retrieved. I selected a random IP address online and got the following data after printing data_str.
{ "ip": "49.82.4.245", "is_eu": false, "city": "Jinhu", "region": "Jiangsu", "region_code": "JS", "country_name": "China", "country_code": "CN", "continent_name": "Asia", "continent_code": "AS", "latitude": 31.1411, "longitude": 120.978, "postal": null, "calling_code": "86", "flag": "https://ipdata.co/flags/cn.png", "emoji_flag": "\ud83c\udde8\ud83c\uddf3", "emoji_unicode": "U+1F1E8 U+1F1F3", "asn": { "asn": "AS4134", "name": "No.31,Jin-rong Street", "domain": null, "route": "49.64.0.0/11", "type": "isp" }, "languages": [ { "name": "Chinese", "native": "\u4e2d\u6587" } ], "currency": { "name": "Chinese Yuan", "code": "CNY", "symbol": "CN\u00a5", "native": "CN\u00a5", "plural": "Chinese yuan" }, "time_zone": { "name": "Asia/Shanghai", "abbr": "CST", "offset": "+0800", "is_dst": false, "current_time": "2020-01-16T00:01:09.613933+08:00" }, "threat": { "is_tor": false, "is_proxy": false, "is_anonymous": false, "is_known_attacker": false, "is_known_abuser": false, "is_threat": false, "is_bogon": false }, "count": "14" }
From the following data, we only need “country_name” and “country_code”.
country_name = data['country_name'] country_code = data['country_code'] print(f'Country Name: {country_name} ({country_code})\n')
SAMPLE OUTPUT
Welcome IP Address to Country Name Enter the IP Address 49.82.4.245 Wait looking for the Country Name Country Name: China (CN)
Thank you for reading this blog, if you liked this then please do leave a comment.
Leave a Reply