开发者

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

开发者 https://www.devze.com 2023-02-04 04:03 出处:网络
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:

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:

<RequestColour>
...
</Re开发者_运维知识库questColour>

The response looks like:

<ResponseColourOutput>
...
</ResponseColourOutput>

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


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

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.

Related SO answers for those steps:

  • Generating XML
  • Consuming XML


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

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.

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

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

Hope this helps.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消