2

I'm tring to get the client's ipv6 (if present) or ipv4 address whenever my api end point is hit. Here's the code:

from flask import request
from flask import jsonify

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

But currently, only the ipv4 address of the client is returned. Is it possible to get the ipv6 address of the client ?

Rahul
  • 3,208
  • 8
  • 38
  • 68

1 Answers1

3

You can start your app specifying an ipv6 address

from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def index():
    return "hello there %s" % request.remote_addr

app.run(host='::')

For http://127.0.0.1:5000/ this will yield

hello there ::ffff:127.0.0.1

And output

 * Running on http://[::]:5000/ (Press CTRL+C to quit)
::ffff:127.0.0.1 - - [09/Aug/2018 10:06:45] "GET / HTTP/1.1" 200 -

And for http://localhost:5000/

hello there ::1

And

 * Running on http://[::]:5000/ (Press CTRL+C to quit)
::1 - - [09/Aug/2018 10:11:25] "GET / HTTP/1.1" 200 -
Pax Vobiscum
  • 2,551
  • 2
  • 21
  • 32