获取访客的IP地址 [英] Get IP address of visitors
问题描述
我正在创建一个网站,用户可以使用 Flask微型框架登录和下载文件(根据 Werkzeug )使用Python(在我的情况下是2.6)。
我需要在登录时获取用户的IP地址(用于记录目的)。
有谁知道如何做到这一点?当然有一种方法可以用Python做到这一点吗?
请参阅如何访问Request对象,然后从这个相同的Request对象获取属性 remote_addr
代码示例
from flask导入请求$ b $ from flask导入jsonify
$ b $ app_route(/ get_my_ip,methods = [GET])
def get_my_ip ():
return jsonify({'ip':request.remote_addr}),200
欲了解更多信息,请参阅 Werkzeug文档。
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.
这篇关于获取访客的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!