python - 如何在 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 ......)。
有任何想法吗?
解决方案
我想我找到了些东西:
通过使用 Flask import objectify,我可以将 XML 文件作为对象读取,然后对其进行调整。完成后,我可以简单地将其转换回 XML 格式的字符串。
推荐阅读
- python - 从图像中去除“噪声”或“孔洞”
- react-native - 管理依赖于道具的样式的最佳方法是什么?
- javascript - 数据表自定义过滤器可在单击按钮时删除具有重复数据的行
- c# - 将 .net Framework 4.5 MVC 应用程序重定向到 /authorize Microsoft Oauth 2.0 端点以检索授权代码
- android - 当运行 ionic cordova run android 时,命令提示符上显示 tranformclasses 错误
- django - 将生产 Django 服务器 + Postgres 数据库复制到相同的 Linode 服务器作为备份
- arrays - 使用 .each 将数组转换为哈希数组
- python - Python从循环中的数据框创建字典
- javascript - 获取javascript中选定选项的值
- c# - 在默认 OS 查看器应用程序中打开从 CEFSharp 下载的文件