How to get IP address of visitors (Flask and Python)

Imports:

from flask import request, jsonify

Flask endpoint and code:

@app.route("/get_my_ip", methods=["GET"])
def get_my_ip():
    return jsonify({'ip': request.remote_addr}), 200

request.remote_addr contains the strong of the IPv4 Address

Another way:

from flask import request
request.environ.get('HTTP_X_REAL_IP', request.remote_addr)

Stack overflow post: https://stackoverflow.com/questions/3759981/get-ip-address-of-visitors-using-flask-for-python

Leave a Reply

Your email address will not be published. Required fields are marked *