如何在 Python 中发送 xml 请求并接收 xml 响应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4660191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:48:05  来源:igfitidea点击:

How do I send an xml request and receive an xml response in Python?

pythonxml

提问by Thierry Lam

I'm currently using a WS where I send an XML Request to a url and then receive an XML Response. The request might look like the following:

我目前正在使用一个 WS,我将一个 XML 请求发送到一个 url,然后接收一个 XML 响应。该请求可能如下所示:

<RequestColour>
...
</RequestColour>

The response looks like:

响应如下:

<ResponseColourOutput>
...
</ResponseColourOutput>

Which libraries should I use in Python to send those xml requests and receive the responses back?

我应该在 Python 中使用哪些库来发送这些 xml 请求并接收响应?

采纳答案by Daniel DiPaolo

You can use the urllib2module that comes with Python to make requests to a URL that can consume this.

您可以使用urllib2Python 附带的模块向可以使用它的 URL 发出请求。

The Python website has a decent tutorial on how to use this module to fetch internet resources.The next step is learning how to generate/consume XML.

Python 网站上有一个关于如何使用此模块获取 Internet 资源的不错的教程。下一步是学习如何生成/使用 XML。

Related SO answers for those steps:

这些步骤的相关 SO 答案:

回答by Santa

If the XML processing you do is simple, try looking at the built-in API, xml.etree.ElementTree.

如果您进行的 XML 处理很简单,请尝试查看内置 API xml.etree.ElementTree.

回答by KevinDTimm

I found thissite to be invaluable to all my python learning.

我发现这个网站对我所有的 Python 学习都是无价的。

In your specific case, try here.

在您的特定情况下,请在此处尝试。

回答by Edmon

I have posted a small example of plain XML request and response in Python here:

我在这里发布了一个用 Python 编写的纯 XML 请求和响应的小例子:

http://it.toolbox.com/blogs/lim/request-get-reply-and-display-xml-in-python-beauty-of-simplicity-49791

http://it.toolbox.com/blogs/lim/request-get-reply-and-display-xml-in-python-beauty-of-simplicity-49791

Please not that my example posts arbitrary XML and gets a valid XML but that informs you of an error because the content of the request was not recognized.

请注意,我的示例发布了任意 XML 并获取了有效的 XML,但由于无法识别请求的内容,因此会通知您错误。

You could use your own URL and XML or adjust not to send XML request and just parse the response against the provided XML.

您可以使用自己的 URL 和 XML,或者调整为不发送 XML 请求,而仅根据提供的 XML 解析响应。

I would also suggest looking into eBays example for their x.Commerce XML API accessed from Python.

我还建议查看 eBays 示例,了解他们从 Python 访问的 x.Commerce XML API。

Hope this helps.

希望这可以帮助。