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.
Join the DZone community and get the full member experience.
Join For FreePython 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 has the ability to 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 language 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 on trying to understand the syntax, and 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 or Linux or MacOS. Python is also adaptable to various fields, such as web server, 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 source to extend the functionality in 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 API 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 the developers to geolocate an IP address in real time. Developers can find out plenty of information about an IP address, such as the country, region, and city that the IP address from, the ISP that the IP address belongs to, is 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 on 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. Users are able to hide their real IP address by using a VPN and are able to 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 countries or regions. Beside 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 from how to obtain your API key through the IP2Location.io dashboard, setting up the key and calling the API in Python, and finally manipulating 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 instal
l requests
2. Login 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 on. 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.")
Conclusion
To wrap it up, Python is simple to use yet powerful. Its comprehensive libraries and modules, easiness to learn and understand, and adaptability while having cross-platform compatibility enables developers to build a user-friendly and powerful application. By integrating the IP2Location.io API with Python, developers have more options when building their application — for example, taking actions based on the IP address’s country, or if the IP address belongs to a proxy or not. For more information about the API, you can visit IP2Location.io API documentation to find out more.
Opinions expressed by DZone contributors are their own.
Comments