获取访客的IP地址 [英] Get IP address of visitors

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

问题描述

我正在创建一个网站,用户可以使用 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屋!

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