【问题标题】:spin up a simple web server that prints post data to console启动一个简单的 Web 服务器,将发布数据打印到控制台
【发布时间】:2017-08-29 00:00:05
【问题描述】:

我在 python 中找到了 numbersimple web servers,它将启动一个快速服务器并简单地响应请求。我需要的是一个服务器,它不仅可以打印每个请求的 IP 地址、时间戳、方法和响应代码 (127.0.0.1 - - [28/Aug/2017 10:42:11] "POST / HTTP/1.1" 200 -),而且如果是 POST 请求,我需要它来打印 POST 数据。

例如,如果我在消息正文中发送带有{"foo":"bar"} 的POST 请求,我希望服务器在响应之前将127.0.0.1 - - [28/Aug/2017 10:42:11] "POST / HTTP/1.1" 200 - {"foo":"bar"} 打印到控制台。

我不确定如何修改上面的任何链接选项来执行此操作。如果有另一个简单的选项,那也可以。

【问题讨论】:

  • 什么样的数据?只是 JSON?形式?文件?有什么具体的,还是以上所有的?
  • @scnerd 仅 JSON
  • Web.py 使用this answer? 打印而不是返回数据。

标签: python webserver


【解决方案1】:

要打印发送到服务器的任何 JSON,请为自己构建一个基本的 catch-all endpoint,然后从中打印 JSON。在烧瓶中,如下所示:

import logging
from flask import Flask, request
app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'], defaults={'path': ''})
@app.route('/<path:path>', methods=['POST', 'GET'])
def index(path):
    print("HTTP {} to URL /{} received JSON {}".format(request.method, path, request.get_json()))
    return "True"

这是我对服务器的调用:

In [15]: requests.post('http://localhost:5000/', json={'a': 1})
Out[15]: <Response [200]>

In [16]: requests.post('http://localhost:5000/some/endpoint', json={'a': 1})
Out[16]: <Response [200]>

In [17]: requests.get('http://localhost:5000/', json={'a': 1})
Out[17]: <Response [200]>

这是服务器输出:

In [7]: app.run(host='0.0.0.0')
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
HTTP POST to URL / received JSON {'a': 1}
127.0.0.1 - - [28/Aug/2017 11:57:49] "POST / HTTP/1.1" 200 -
HTTP POST to URL /some/endpoint received JSON {'a': 1}
127.0.0.1 - - [28/Aug/2017 11:57:51] "POST /some/endpoint HTTP/1.1" 200 -
HTTP GET to URL / received JSON {'a': 1}
127.0.0.1 - - [28/Aug/2017 11:57:55] "GET / HTTP/1.1" 200 -

原答案

一个简单的装饰器就可以解决问题:

from flask import Flask, request
app = Flask(__name__)

def print_if_post(*args, **kwargs):
    def inner_decorator(f):
        def inner(*args, **kwargs):
            if request.method == 'POST':
                json = request.get_json()
                print("JSON Data: {}".format(json))

            return f(*args, **kwargs)

        return app.route(*args, **kwargs)(inner)
    return inner_decorator

此装饰器的功能与 app.route 完全相同,但会打印发送到其端点的任何 JSON 数据:

@print_if_post('/', methods=['POST', 'GET'])
def index():
    return "True"

使用以下代码调用:

In [4]: requests.get('http://localhost:5000/')
Out[4]: <Response [200]>

In [5]: requests.post('http://localhost:5000/', json={'a': 1})
Out[5]: <Response [200]>

服务器输出:

In [2]: app.run(host='0.0.0.0')
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [28/Aug/2017 11:03:11] "GET / HTTP/1.1" 200 -
JSON Data: {'a': 1}
127.0.0.1 - - [28/Aug/2017 11:03:23] "POST / HTTP/1.1" 200 -

【讨论】:

  • 没有Flask就没有办法做到这一点吗?
  • 就像您链接的示例一样,使用some help from here,您可以编写一个最小的服务器来解析和打印 POST 请求中的 JSON 数据。如果没有有关您尝试编写的服务器的更多详细信息,我不确定我能否给您一个好的答案。您是否主要尝试提供文件?如果是这样,你为什么关心 JSON POST 数据?您正在编写一个基本的 Web 应用程序吗?如果是这样,您为什么不使用更完整的服务器,例如 flask、django 或 web.py(根据 @atwalsh04)?
  • 我实际上是在尝试编写一个接受请求的服务器,将 POST 数据打印到控制台(如果有的话),以 20X 响应,并且绝对不做任何其他事情。重点是监控我编写的另一个应用程序正在发送哪些类型的请求。
  • 按照当时的建议使用 Flask 或 Web.py。它隐藏了很多“服务器”的废话,让你只写你想要的逻辑。如果依赖项真的不是一种选择,那么您可能会遇到一些困难。我将为该用例提供更一般的建议来编辑答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 2013-12-03
  • 2012-05-29
  • 1970-01-01
  • 2019-01-24
相关资源
最近更新 更多