How to detect Public Proxy in Flask

IP2Location
3 min readApr 7, 2021

--

Introduction

Flask is a lightweight web framework written in Python which makes it easy to develop a web application. It comes with a number of libraries to help web development. It is essential for developers to choose which libraries to use. This is because a good library can help in developing a good website.

Sometimes, developers or web admins will need to manage the accessibility of certain webpages. For example, they may want to block visitors who are using public proxy servers from visiting certain pages. In this case, IP2Proxy Python library may come in handy. In this article, we will show you how to redirect visitors in Flask using the IP2Proxy Python library and the free IP2Proxy LITE BIN database.

Installation

Before we start, you will be required to install the IP2Proxy Python library in your machine. You can install the library using the following command:

pip install IP2Proxy

You will also be required to download the free IP2Proxy LITE BIN database. Rest assured, you can always get the free database from here: https://lite.ip2location.com/ip2proxy-lite.

Steps

  1. Open your Flask application for editing.

2. Import the IP2Proxy Python library like this: import IP2Location

3. Declare your database path and load the database like this:

db = IP2Proxy.IP2Proxy() 
db.open("/path/to/IP2PROXY-LITE-PX1.BIN")

4. After that, you will need to create a function to listen on a specific path and catch the visitor’s IP address. Then the function will redirect visitors to a different page if the visitor is using a public proxy. For example, if you wish to let the function to check on the home path, you can do like the following:

@app.route('/', methods=['GET'])
def main():
global record
if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
ip = request.environ['REMOTE_ADDR']
else:
ip = request.environ['HTTP_X_FORWARDED_FOR']
if ip == '127.0.0.1': # Happened in Windows, if the IP got is 127.0.0.1, need to substitute with real IP address
ip = REAL_IP_ADDRESS
record = db.get_all(ip)
if (record['country_short'] is not '-'): # IP2PROXY-LITE-PX1 contain only PUB proxy type IP addresses.
return redirect(LINK_TO_PAGE_FOR_VPN_VISITOR, code=302)
else:
return redirect(LINK_TO_HOME_PAGE, code=302)

5. Next, you will need to create another function to display the page to the visitor which is using a public proxy. For example, if you have created a page template for this, you can load the template as shown below:

@app.route('/access-denied')
def access_deny():
return render_template(YOUR_TEMPLATE_NAME)

6. Finally, save and push the changes to the production server.

Note: The free IP2Proxy LITE BIN database contains only public proxy IP addresses. If you wish to validate for other proxy type IP addresses such as Virtual Private Network (VPN) or Search Engine Spider (SES), you will need to get the IP2Proxy commercial database from here: https://ip2location.com/database/ip2proxy

Full code of Flask application is shown as below:

import IP2Proxy
from flask import Flask, redirect, render_template, request

app = Flask(__name__)

db = IP2Proxy.IP2Proxy()
db.open("IP2PROXY-LITE-PX1.BIN")

@app.route('/', methods=['GET'])
def main():
global record
if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
ip = request.environ['REMOTE_ADDR']
else:
ip = request.environ['HTTP_X_FORWARDED_FOR']
if ip == '127.0.0.1': # Happened in Windows, if the IP got is 127.0.0.1, need to substitute with real IP address
ip = REAL_IP_ADDRESS
record = db.get_all(ip)
if (record['country_short'] is not '-'): # IP2PROXY-LITE-PX1 contain only PUB proxy type IP addresses.
return redirect(LINK_TO_PAGE_FOR_VPN_VISITOR, code=302)
else:
return redirect(LINK_TO_HOME_PAGE, code=302)

@app.route('/access-denied')
def access_deny():
return render_template(YOUR_TEMPLATE_NAME)

Originally published at https://blog.ip2location.com on April 7, 2021.

--

--

IP2Location
IP2Location

Written by IP2Location

IP2Location™ is a non-intrusive geo IP solution to help you to identify visitor’s geographical location using a proprietary IP address lookup database.

No responses yet