Your resource for web content, online publishing
and the distribution of digital products.
S M T W T F S
1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
 
 
 
 
 
 
 
 
 
 
20
 
21
 
22
 
23
 
24
 
25
 
26
 
27
 
28
 
29
 
30
 
 
 
 
 
 

How to Detect Proxies on Your Site with Flask Backend and IPQuery API

DATE POSTED:September 7, 2024

In this tutorial, we'll build a Flask application that checks whether the incoming requests are from proxy servers using the IPQuery API. We’ll also render a simple HTML page to display the results of the proxy check dynamically.

Requirements:

Python 3.x Flask requests library to interact with the IPQuery API

\ You can install the required libraries using pip:

pip install flask requests Step 1: Setting Up the Flask Backend

First, let’s set up a basic Flask application. We’ll create an endpoint that receives the visitor's IP address and uses the IPQuery API to check if the IP is associated with any proxy services.

\ We will render the results on an HTML page using Jinja2's {{ }} syntax to inject data dynamically.

app.py

from flask import Flask, render_template, request import requests app = Flask(__name__) # Helper function to query IP info from IPQuery API def check_ip(ip): api_url = f"https://api.ipquery.io/{ip}" response = requests.get(api_url) return response.json() # Route to handle the proxy check @app.route('/') def index(): visitor_ip = request.remote_addr # Get the IP address of the visitor ip_data = check_ip(visitor_ip) # Fetch IP details from IPQuery is_proxy = ip_data['risk']['is_proxy'] # Check if the IP is a proxy # Render HTML with the result return render_template('index.html', ip=visitor_ip, ip_data=ip_data, is_proxy=is_proxy) if __name__ == '__main__': app.run(debug=True)

\

Step 2: HTML Template with Jinja2

Now, let’s create an HTML template that will display the visitor’s IP and whether or not it is a proxy. We’ll use Flask’s built-in template engine Jinja2 to dynamically inject data.

\ templates/index.html

Proxy Detection

IP Address Information

Your IP Address: {{ ip }}

Proxy Status

{% if is_proxy %} This IP is associated with a proxy service. {% else %} This IP is not associated with a proxy service. {% endif %}

Location Information

  • Country: {{ ip_data['location']['country'] }}
  • City: {{ ip_data['location']['city'] }}
  • Latitude: {{ ip_data['location']['latitude'] }}
  • Longitude: {{ ip_data['location']['longitude'] }}
  • Timezone: {{ ip_data['location']['timezone'] }}

Risk Analysis

  • Is VPN: {{ ip_data['risk']['is_vpn'] }}
  • Is Tor: {{ ip_data['risk']['is_tor'] }}
  • Is Data Center: {{ ip_data['risk']['is_datacenter'] }}

\

Step 3: Testing the Application

Once the Flask application and the HTML template are set up, you can run the application by executing:

python app.py

\ Open your browser and navigate to http://127.0.0.1:5000/. The page will show the visitor’s IP address, location details, and whether or not the IP is associated with a proxy.

\ \