Skip to content

Instantly share code, notes, and snippets.

@ruanbekker
Created June 7, 2017 20:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruanbekker/b745d6cb3bf56d4105f08b19eac6d8fc to your computer and use it in GitHub Desktop.
Save ruanbekker/b745d6cb3bf56d4105f08b19eac6d8fc to your computer and use it in GitHub Desktop.
Python Flask Show Client IP
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/ip', methods=['GET'])
def name():
return request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
#return jsonify({'ip': request.remote_addr}), 200
#return jsonify({'ip': request.environ['REMOTE_ADDR']}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
@math77
Copy link

math77 commented Jul 21, 2018

Thank you, it helped a lot.

Copy link

ghost commented Sep 6, 2018

this is return the localhost ip, how to get the public ip with flask?

@phalbert
Copy link

this is return the localhost ip, how to get the public ip with flask?

Perhaps you were running this in development....if you put it on a server (like gunicorn) ... it should return the ip of the client

@dansobolev
Copy link

this is return the localhost ip, how to get the public ip with flask?

Perhaps you were running this in development....if you put it on a server (like gunicorn) ... it should return the ip of the client

and how i can change it?

@KartoffelCheetah
Copy link

this is return the localhost ip, how to get the public ip with flask?

Perhaps you were running this in development....if you put it on a server (like gunicorn) ... it should return the ip of the client

and how i can change it?

Clearly the IP is the one sent with the HTTP request, there are no other IPs as far as Flask is concerned.
https://werkzeug.palletsprojects.com/en/0.15.x/wrappers/#werkzeug.wrappers.BaseRequest.remote_addr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment