首页 > 解决方案 > 如何在 GET 请求(FLASK)上返回现有的 XML 文件

问题描述

我目前正在努力处理一些 Python Flask 代码。我有一个现有的 xml 文件,其中有一些值,每当在某个 URL 上完成 GET 请求时,我想发送这些值。

我似乎无法将我已经存在的 XML 发送回发送 GET 请求的客户端。谁能帮帮我?

from app import app
from flask import request, Response, render_template, make_response


@app.route('/')
def index():
    return "Hello World, this is the index page"

@app.route('/inventory')
def inventory():
    if(request.method == "GET"):
        #Here should the return happen of the existing xml-file

我的 XML 文件与此代码不在同一目录中。如果此代码在“localhost/app/routes.py”中,则 xml 文件在“localhost/resources/resources.xml”中。

我尝试了不同的方法,但似乎都没有效果(使用烧瓶的响应、render_template、make_response ......)。

有任何想法吗?

标签: pythonxmlflask

解决方案


我想我找到了些东西:

通过使用 Flask import objectify,我可以将 XML 文件作为对象读取,然后对其进行调整。完成后,我可以简单地将其转换回 XML 格式的字符串。


推荐阅读