Geolocate an IP Address in Python

Cloudmersive
2 min readJan 31, 2022

--

In this article we’ll walk through how you can connect with an iteration of the Cloudmersive Validate API which can be very useful in security & UX applications. The /validate/ip/geolocate API will allow you to identify the country, state & province, city, zip & postal code, etc. for any given IP address. Let’s look at how you can connect in Python.

The first step you’ll need to complete is copying the below Python SDK installation command + additional code to begin the callback function:

pip install cloudmersive-validate-api-clientfrom __future__ import print_function
import time
import cloudmersive_validate_api_client
from cloudmersive_validate_api_client.rest import ApiException
from pprint import pprint

After that, you can authorize your Cloudmersive API key with the next snippet:

# Configure API key authorization: Apikey
configuration = cloudmersive_validate_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'

And for the final step, all that’s left is copying in the rest of the callback function:

# 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)

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet