This content originally appeared on HackerNoon and was authored by dvr
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Proxy Detection</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
padding: 20px;
}
.container {
max-width: 600px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.status {
color: {% if is_proxy %} red {% else %} green {% endif %};
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h1>IP Address Information</h1>
<p><strong>Your IP Address:</strong> {{ ip }}</p>
<h2>Proxy Status</h2>
<p class="status">
{% if is_proxy %}
This IP is associated with a proxy service.
{% else %}
This IP is not associated with a proxy service.
{% endif %}
</p>
<h2>Location Information</h2>
<ul>
<li><strong>Country:</strong> {{ ip_data['location']['country'] }}</li>
<li><strong>City:</strong> {{ ip_data['location']['city'] }}</li>
<li><strong>Latitude:</strong> {{ ip_data['location']['latitude'] }}</li>
<li><strong>Longitude:</strong> {{ ip_data['location']['longitude'] }}</li>
<li><strong>Timezone:</strong> {{ ip_data['location']['timezone'] }}</li>
</ul>
<h2>Risk Analysis</h2>
<ul>
<li><strong>Is VPN:</strong> {{ ip_data['risk']['is_vpn'] }}</li>
<li><strong>Is Tor:</strong> {{ ip_data['risk']['is_tor'] }}</li>
<li><strong>Is Data Center:</strong> {{ ip_data['risk']['is_datacenter'] }}</li>
</ul>
</div>
</body>
</html>
\
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.
\ \
This content originally appeared on HackerNoon and was authored by dvr
dvr | Sciencx (2024-09-07T10:31:48+00:00) How to Detect Proxies on Your Site with Flask Backend and IPQuery API. Retrieved from https://www.scien.cx/2024/09/07/how-to-detect-proxies-on-your-site-with-flask-backend-and-ipquery-api/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.