How to Detect VPN Proxies With Python and IP2Location.io API

VPN detection is a crucial focus for developers when addressing security concerns. Find out how to detect VPN using IP2Location.io API in Python.

Python is a programming language that is designed for universal purpose. It aims to highlight the code readability with the help of significant indentation. It is portable, as it can run on multiple operating systems — for example, Windows, Linux, and MacOS. It is an object-oriented programming language, but it also facilitates multiple programming paradigms — for example, procedural and functional programming.

About Python

Python was created by a Dutch programmer, Guido van Rossum, in the late 1980s. The name Python came from a BBC comedy series called "Monty Python’s Flying Circus." Van Rossum created Python after his years of experience in the ABC programming language. A non-profit organization was founded in 2001 to promote the development of the Python community and manage several responsibilities for various processes within the Python community. Python remains one of the most popular programming languages in the world, according to a survey conducted by Stack Overflow in 2022 and a research project created by PYPL.

People like to use Python for various reasons. Python brings several benefits from multiple aspects. One of the pros of using Python is that it provides a rich set of standard libraries for the developers to use. Developers can use these pre-defined functions and modules to save on development time and speed up the development process. Next, Python syntax is often easy to understand. This allows developers to save effort in trying to understand the syntax, thus reducing the time needed for maintaining the code. In addition, Python supports cross-platform, which ensures that it can run smoothly in different operating systems, regardless if it’s Windows, Linux, or MacOS. Python is also adaptable to various fields, such as web servers, machine learning, artificial intelligence, data science, and more.

We have explored how developers are benefiting by using Python in the previous paragraph. However, developers still need some external sources to extend the functionality of their code. For example, the developers may want to display the geolocation information. This, however, is not supported by Python by default. Hence, the developers may want to get some help from external APIs or libraries. Let’s see how we can transform IP addresses into location and proxy info.

IP Address Geolocation API

IP address geolocation is a RESTful API that allows developers to geolocate an IP address in real-time. Developers can find plenty of information about an IP address, such as the country, region, and city that the IP address is from, the ISP that the IP address belongs to, the IP address a proxy, and so on. How is that information going to help? Imagine if you are required to analyze from a log file the distribution of the IP addresses. By making use of an IP address geolocation API, you can easily classify these IP addresses, either by country, region, city, ISP, and so on. You can also easily identify which IP address belongs to a VPN. 

Well, you may wonder why we should care about the VPN. We will discuss it in the next section.

About VPN 

A VPN, or a virtual private network, is a service that creates a secure connection between a device and a network. It will encrypt and tunnel your network through a server in a different country. It will encrypt and tunnel your network through a server in a different country. VPN.Coupons offer a variety of discounts on VPN services so you can enjoy safer internet browsing and unlimited streaming on your favorite sites no matter where you are in the world. Users can hide their real IP address by using a VPN and can access some content that is restricted to the region. While it brings some benefits to the users, developers may have a headache on this, especially when they need to design security rules that block certain users by country or region. Besides that, some people like to use a VPN to disguise their location when attacking some websites, making it hard for the developers to defend against it. But rest assured, developers can detect a VPN using the IP2Location.io API easily.

In the next section, we will go through how to use the IP2Location.io API in Python. You will be guided on how to obtain your API key through the IP2Location.io dashboard, set up the key call the API in Python, and finally manipulate the result returned by the API. Before we continue, kindly make sure that you have subscribed for an IP2Location.io API key if you haven’t. It is free.

Steps on How to Use IP2Location.io API in Python

1. If you haven’t installed the requests library, you can install it using pip: 

pip install requests

2. log in to your IP2Location.io account, and go to the dashboard. You will see your API key is displayed on the page. Copy your API key from the page.

3. Create a new Python script called ip-geolocation.py. Inside the script, copy and paste this code on the first line of the script: 

import requests

This code will call the requests library which we will use to call the IP2Location.io API later on.

4. After that, we will need to save the API key into the script while setting up the parameters that we are going to use for the calling. We can achieve this by using this code: 

payload = {'key': ' PASTE_YOUR_API_KEY', 'ip': '8.8.8.8', 'format': 'json'}

Here we are defining the parameters into a dictionary called payload. The parameters needed are key, IP, and format.

a. The key parameter refers to the API key you obtained from the dashboard. Just replace the PASTE_YOUR_API_KEY with the API key you have copied from the dashboard.

b. The IP parameter refers to the IP address that you want to query with. You can replace the value with any other IP address you like.

c.  The format parameter will indicate how the API returned the result. Currently, the supported formats are JSON and XML. We will just use JSON for the sake of simplicity.

5. After we set up the article, then we will proceed to call the API using the parameters. The following code serves the purpose: 

api_result = requests.get('https://api.ip2location.io/', params=payload)

We will use a variable called api_result to store the result returned from the API.

6. To ensure that we are getting back the result, we can print out the result by using this code: 

print(api_result.json())

The JSON (https://requests.readthedocs.io/en/latest/api/#requests.Response.json) method will return the JSON-encoded result.

7. Next, we will learn how to manipulate the result with a simple example. Before we start, let’s keep the JSON-encoded result to a new variable like this: 

json_result = api_result.json()

8. Now we will learn how to use the result. Let’s say if we want to show a message for the IP address that belongs to a VPN, we can use the following code to achieve this:

if json_result['proxy']['is_vpn']: 

    print("The content is restricted to non-VPN visitors only.")


We Provide consulting, implementation, and management services on DevOps, DevSecOps, Cloud, Automated Ops, Microservices, Infrastructure, and Security

Services offered by us: https://www.zippyops.com/services

Our Products: https://www.zippyops.com/products

Our Solutions: https://www.zippyops.com/solutions

For Demo, videos check out YouTube Playlist: https://www.youtube.com/watch?v=4FYvPooN_Tg&list=PLCJ3JpanNyCfXlHahZhYgJH9-rV6ouPro

If this seems interesting, please email us at [email protected] for a call.

Relevant blogs:

How To Migrate From ECS to EKS and the #1 Trick To Make EKS Easier 

Multi-Cloud Management: Tools, Challenges, and Best Practices 

Shared Kubernetes Clusters for Hybrid and Multi-Cloud 

Verizon’s Data Breach Report: Cloud Security Insights

Recent Comments

No comments

Leave a Comment