【发布时间】:2018-12-09 18:08:06
【问题描述】:
我正在使用 pythonanywhere.com 运行我的代码, 我在 index.html 中使用了一个 post 表单,我有一个 log.txt 文件,我想记录用户的 info ip 地址,下面我想要 post form。 https://ibb.co/esiJhJ
【问题讨论】:
标签: html python-3.x logging flask
我正在使用 pythonanywhere.com 运行我的代码, 我在 index.html 中使用了一个 post 表单,我有一个 log.txt 文件,我想记录用户的 info ip 地址,下面我想要 post form。 https://ibb.co/esiJhJ
【问题讨论】:
标签: html python-3.x logging flask
您可以从request.remote_addr 获取IP 地址,并使用python 默认的logging 库,您可以轻松地将IP 写入文件。
代码示例
import logging
from flask import request
# configure your logging and let the file name is text.log
logging.basicConfig(filename='text.log',
level=logging.INFO)
@app.route("/your_path", methods=["POST"])
def example():
logging.info("ip: {}".format(request.remote_addr))
您还可以修改写入格式。更多详情请访问here
【讨论】: