How to geolocate an IP in Python
1 min readAug 29, 2019
Let’s dive right in. Use pip installer to add Cloudmersive’s Validate API Client:
pip install git+https://github.com/Cloudmersive/Cloudmersive.APIClient.Python.Validate.git
Then all that’s left is to call i_p_address_post:
from __future__ import print_function
import time
import cloudmersive_validate_api_client
from cloudmersive_validate_api_client.rest import ApiException
from pprint import pprint# Configure API key authorization: Apikey
configuration = cloudmersive_validate_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API class
api_instance = cloudmersive_validate_api_client.IPAddressApi(cloudmersive_validate_api_client.ApiClient(configuration))
value = 'value_example' # str | IP address to geolocate, e.g. \"55.55.55.55\". The input is a string so be sure to enclose it in double-quotes.try:
# Geolocate an IP address
api_response = api_instance.i_p_address_post(value)
pprint(api_response)
except ApiException as e:
print("Exception when calling IPAddressApi->i_p_address_post: %s\n" % e)
And that’s all there is to it.
Let’s look at an example result for 11.11.11.11:
{
"CountryCode": "US",
"CountryName": "United States",
"City": "Bullard",
"RegionCode": "TX",
"RegionName": "Texas",
"ZipCode": "75757",
"TimezoneStandardName": "America/Chicago",
"Latitude": 32.1095,
"Longitude": -95.3342
}
Note that we are provided with quite a bit of useful information for the IP address in question, including the city, country, and geographical coordinates.