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

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

问题描述

我正在制作一个网站,用户可以使用 Flask 微框架登录并下载文件(基于 Werkzeug),它使用 Python(在我的情况下为 2.6).

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).

我需要在用户登录时获取其 IP 地址(用于登录目的).有谁知道如何做到这一点?肯定有办法用 Python 做到这一点吗?

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?

推荐答案

查看如何访问Request 对象 然后从这个相同的Request 对象中获取属性remote_addr.

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

代码示例

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

有关详细信息,请参阅 Werkzeug 文档.

For more information see the Werkzeug documentation.

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

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