2

Is there a preferred python module that could help me to send XML through a HTTP request and be able to parse the returning XML?

3
  • Will it be a SOAP message or just something-XML message? For SOAP integration with python see suds. Commented May 22, 2012 at 18:36
  • Relevant for parsing XML: stackoverflow.com/q/1912434/208339. I also like ElementTree. Commented May 22, 2012 at 18:48
  • The XML will be in the format of something simple like <status>0</status> or several nestings. Commented May 22, 2012 at 18:52

1 Answer 1

6

One way would be to use urllib2:

r = urllib2.Request("http://example.com", data="<xml>spam</xml>",
                     headers={'Content-Type': 'application/xml'})
u = urllib2.urlopen(r)
response = u.read()

Note that you have to set the content-type header, or the request will be sent application/x-www-form-urlencoded.

If that's too complicated for you, then you can also use the requests library.

For parsing the response lxml is a great library, but elementtree will also do.

Sign up to request clarification or add additional context in comments.

6 Comments

I strongly recommend requests in all cases.
I keep getting the error badstatusline caused by the status being empty. Is there any way to ignore this or get the responding XML?
that means your server probably doesn't return a valid http status.
why is that a problem? Is there any other module besides httplib for sending http requests?
yes, as i wrote you should try the requests library, it possibly can deal with a bad status better. but if you have control over the server, you should check if it's returning a valid status or why it isn't.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.