Last modified: Sep 13, 2026
Build an IP Grabber in Python
Creating an IP grabber in Python is a great way to understand web development basics. You can capture the IP address of anyone who visits your page. This guide will walk you through building one from scratch using Flask and the request library.
What Is an IP Grabber?
An IP grabber is a tool that captures the IP address of a user visiting a specific web page. It works by logging the IP associated with each incoming HTTP request. Developers often use this for analytics or security purposes.
You will learn how to create a simple version using Python. The process involves setting up a small web server and handling requests.
Why Use Python for an IP Grabber?
Python is beginner-friendly and has powerful libraries like Flask. These make creating web applications easy. You do not need advanced knowledge to get started.
Required Libraries
Before writing code, install the necessary packages. You need Flask to run the web server. Use pip to install it:
pip install flask
This command installs Flask globally on your system.
Setting Up the Project
Create a new folder for your project. Inside it, create a file called app.py. This file will contain all your code.
Basic Flask App Structure
Start by importing Flask and request from the flask module. Then initialize the app:
from flask import Flask, request
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, World!"
if __name__ == "__main__":
app.run(debug=True)
This sets up a basic Flask app. When you visit http://localhost:5000, it returns "Hello, World!".
Extracting the IP Address
To grab the IP address, use the request.remote_addr attribute. It gives the IP of the current visitor.
Modify the route to display the IP address instead:
@app.route("/")
def home():
ip_address = request.remote_addr
return f"Your IP Address is: {ip_address}"
Now when someone visits your page, they see their own IP address displayed.
Storing Captured IPs
You might want to save the captured IP addresses. You can write them to a text file.
Add a function to log the IP address:
@app.route("/")
def home():
ip_address = request.remote_addr
with open("ip_log.txt", "a") as file:
file.write(ip_address + "\n")
return f"Your IP Address is: {ip_address}"
This appends each new IP to a file named ip_log.txt. Each IP appears on a new line.
Handling Proxy IPs
If your app runs behind a proxy, request.remote_addr may return the proxy IP. To fix this, check the X-Forwarded-For header.
@app.route("/")
def home():
ip_address = request.headers.get("X-Forwarded-For", request.remote_addr)
with open("ip_log.txt", "a") as file:
file.write(ip_address + "\n")
return f"Your IP Address is: {ip_address}"
This checks for the forwarded IP first. If none exists, it falls back to the direct connection IP.
Running the Application
Save your file and run it from the terminal:
python app.py
Your server starts on port 5000. Open your browser and go to http://localhost:5000. You should see your IP address displayed.
Testing the IP Grabber
Try accessing the page from different browsers or devices. Each visit logs a new IP address. Check the ip_log.txt file to see captured IPs.
192.168.1.1
10.0.0.2
Each line represents a unique visitor’s IP address.
Security and Ethical Considerations
Always inform users that their IP is being logged. Do not misuse collected data. Respect privacy laws like GDPR.
Use this project only for educational purposes. Never deploy it without consent.
Conclusion
You have built a simple IP grabber in Python using Flask. You learned how to extract and store IP addresses. You also handled proxy scenarios. This project helps beginners understand web request handling and response management.
Experiment further by adding timestamps or saving logs in a database. Keep learning and stay ethical!