如何在烧瓶中获取ipv6地址?

时间:2018-08-09 06:10:08

标签: python networking flask ip

每当我的api端点被命中时,我都想获取客户端的ipv6(如果存在)或ipv4地址。这是代码:

from flask import request
from flask import jsonify

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

但是目前,仅返回客户端的ipv4地址。是否可以获取客户端的ipv6地址?

1 个答案:

答案 0 :(得分:0)

您可以指定ipv6地址启动您的应用

from flask import Flask, request

app = Flask(__name__)

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

app.run(host='::')

对于http://127.0.0.1:5000/,这将产生

  

你好,:: ffff:127.0.0.1

并输出

 * 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 -

对于http://localhost:5000/

  

你好,:: 1

 * Running on http://[::]:5000/ (Press CTRL+C to quit)
::1 - - [09/Aug/2018 10:11:25] "GET / HTTP/1.1" 200 -