使用Flask for Python获取访问者的IP地址 [英] Get IP address of visitors using Flask for Python

查看:868
本文介绍了使用Flask for Python获取访问者的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个网站,用户可以使用烧瓶微框架登录和下载文件 (基于 Werkzeug )使用Python(在我的情况下为2.6).

我需要获得用户登录时的IP地址(出于记录目的). 有谁知道如何做到这一点?当然有办法用Python做到这一点吗?

解决方案

请参见

有关更多信息,请参见 Werkzeug文档. /p>

I'm making a website where users can log on and download files, using the Flask micro-framework (based on Werkzeug) which uses Python (2.6 in my case).

I need to get the IP address of users when they log on (for logging purposes). Does anyone know how to do this? Surely there is a way to do it with Python?

解决方案

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.

这篇关于使用Flask for Python获取访问者的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆