简体   繁体   English

从python使用XML的POST烧瓶服务器

[英]POST flask server with XML from python

I have a flask server up and running on pythonanywhere and I am trying to write a python script which I can run locally which will trigger a particular response - lets say the server time, for the sake of this discussion. 我有一个烧瓶服务器并在pythonanywhere上运行,我正在尝试编写一个python脚本,我可以在本地运行,这将触发一个特定的响应 - 让我们说服务器时间,为了讨论的缘故。 There is tonnes and tonnes of documentation on how to write the Flask server side of this process, but non/very little on how to write something which can trigger the Flask app to run. 有关如何编写此过程的Flask服务器端的文档和吨文档,但非如此/很少有关于如何编写可触发Flask应用程序运行的内容的文档。 I have tried sending XML in the form of a simple curl command eg 我试过以简单的curl命令的形式发送XML,例如

curl -X POST -d '<From>Jack</From><Body>Hello, it worked!</Body>' URL

But this doesnt seem to work (errors about referral headers). 但这似乎不起作用(有关推荐标题的错误)。

Could someone let me know the correct way to compose some XML which can be sent to a listening flask server. 有人能告诉我构建一些可以发送到监听烧瓶服务器的XML的正确方法。

Thanks, 谢谢,

Jack 插口

First, i would add -H "Content-Type: text/xml" to the headers in the cURL call so the server knows what to expect. 首先,我将-H "Content-Type: text/xml"到cURL调用中的标题中,以便服务器知道会发生什么。 It would be helpful if you posted the server code (not necessarily everything, but at least what's failing). 如果您发布服务器代码(不一定是所有内容,但至少是失败的内容)会很有帮助。

To debug this i would use 要调试这个我会用

@app.before_request
def before_request():
    if True:
        print "HEADERS", request.headers
        print "REQ_path", request.path
        print "ARGS",request.args
        print "DATA",request.data
        print "FORM",request.form

It's a bit rough, but helps to see what's going on at each request. 这有点粗糙,但有助于了解每个请求发生了什么。 Turn it on and off using the if statement as needed while debugging. 在调试时根据需要使用if语句打开和关闭它。

Running your request without the xml header in the cURL call sends the data to the request.form dictionary. 在cURL调用中运行没有xml标头的请求会将数据发送到request.form字典。 Adding the xml header definition results in the data appearing in request.data. 添加xml标头定义会导致request.data中出现数据。 Without knowing where your server fails, the above should give you at least a hint on how to proceed. 如果不知道服务器在哪里出现故障,上面的内容应该至少给出一个如何继续的提示。

EDIT referring to comment below: 编辑参考下面的评论:

I would use the excellent xmltodict library. 我会使用优秀的xmltodict库。 Use this to test: 用它来测试:

import xmltodict
@app.before_request
def before_request():
    print xmltodict.parse(request.data)['xml']['From']

with this cURL call: 有了这个cURL电话:

curl -X POST -d '<xml><From>Jack</From><Body>Hello, it worked!</Body></xml>' localhost:5000 -H "Content-Type: text/xml"

'Jack' prints out without issues. '杰克'打印出来没有问题。

Note that this call has been modified from your question- the 'xml' tag has been added since XML requires a root node (it's called an xml tree for a reason..). 请注意,此调用已从您的问题中修改 - 添加了“xml”标记,因为XML需要根节点(出于某种原因,它被称为xml )。 Without this tag you'll get a parsing error from xmltodict (or any other parser you choose). 如果没有此标记,您将从xmltodict(或您选择的任何其他解析器)获得解析错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM