Determine Visitors’ IP Address using Flask in Python

Posted by

Get IP address of visitors using Flask for Python

Get IP address of visitors using Flask for Python

Flask is a web framework for Python that allows you to build web applications quickly and easily. One common task in web development is to get the IP address of visitors to your website. In this article, we will show you how to do this using Flask.

Step 1: Install Flask

If you haven’t already installed Flask, you can do so using pip:

pip install Flask

Step 2: Create a Flask app

Next, create a new Python file and add the following code to create a basic Flask app:

from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def get_ip():
    ip_address = request.remote_addr
    return f"Your IP address is: {ip_address}"

if __name__ == '__main__':
    app.run()

Step 3: Run the Flask app

Save the file and run the Flask app using the following command:

python your_app_file.py

Once the app is running, you can visit http://127.0.0.1:5000 in your browser to see your IP address displayed on the page.

Conclusion

Getting the IP address of visitors to your website is a common task in web development. Using Flask makes it easy to access and display this information. By following the steps outlined in this article, you can quickly get up and running with a basic Flask app that displays the IP address of visitors.