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 BackendFirst, 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 Jinja2Now, 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
Your IP Address: {{ ip }}
{% if is_proxy %} This IP is associated with a proxy service. {% else %} This IP is not associated with a proxy service. {% endif %}
\
Step 3: Testing the ApplicationOnce 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.
\ \
All Rights Reserved. Copyright , Central Coast Communications, Inc.