Ask coding questions

← Back to all posts
How do I get IP of client.
ViLLaNClan (0)

How do I get IP of client in flask python. I tried this:
print(request.environ)
try:

        print(request.environ['HTTP_X_FORWARDED_FOR'].split(',')[-1].strip())
    except KeyError:
        print(request.environ['REMOTE_ADDR'])

It only returned the following:
127.0.0.1
A Google IP

NOTE: This is in testing this is more for IP banning stuff

Commentshotnewtop
PattanAhmed (1014)

@ViLLaNClan Hi,
See the documentation on how to access the Request object and then get from this same Request object, the attribute remote_addr.

Code Example:-

from flask import request
from flask import jsonify

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

For more information, see the Werkzeug documentation

Reference 👆 :- Click here

Hope this helps
Please mark my answer if this helps

ViLLaNClan (0)

@PattanAhmed it just responds with a private IP which is the servers. {"ip":"172.18.0.1"}

PattanAhmed (1014)

@ViLLaNClan Try by searching How do I get IP of a client in Flask in your Web Browser to know more.

With my best wishes
Hope this helps
Please mark my answer if this helps

ViLLaNClan (0)

@PattanAhmed i have done for 2 days im not gonna get anywhere with google, trust me i tried

PattanAhmed (1014)

@ViLLaNClan Oh then you can find someone to help!

ViLLaNClan (0)

@PattanAhmed Thats why im posting on this forum

PattanAhmed (1014)

@ViLLaNClan Wait a minute...
I think @JosephSanthosh can help you!
As he mentioned that to ping him!
Wait for his reply here!
Thanks!

Coder100 (7006)

Hi! You will never be able to get someone's exact IP due to their proxy and such. Right now what is going on is you have gotten your local IP address.

octopyBot (263)

try this:
www.kite.com › python › answers › how-to-get-an-ip-adress-using-Flask-in-Python%20

app = flask. Flask(__name__)
@app. route("/")
def index():
ip_address = flask. request. remote_addr.
return "Requester IP: " + ip_address.
app. run(host="0.0.0.0", port=8080)
ViLLaNClan (0)

@octopyBot name 'flask' is not defined

ViLLaNClan (0)

@ViLLaNClan changed uppercase of first letter this happend type object 'Flask' has no attribute 'request'

ViLLaNClan (0)

@octopyBot i imported flask and it just responded with 127.0.0.1

octopyBot (263)

@ViLLaNClan ok then i think its installing newer software, it might work after a few tries or a refresh. also, for the other 2, you didn't import flask, and you might not have your poetry.lock files, so create those and try again.

also I just realized that your name is "villanclan"...