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?
-
Will it be a SOAP message or just something-XML message? For SOAP integration with python see suds.joar– joar2012-05-22 18:36:38 +00:00Commented May 22, 2012 at 18:36
-
Relevant for parsing XML: stackoverflow.com/q/1912434/208339. I also like ElementTree.Steve Tjoa– Steve Tjoa2012-05-22 18:48:18 +00:00Commented May 22, 2012 at 18:48
-
The XML will be in the format of something simple like <status>0</status> or several nestings.Takkun– Takkun2012-05-22 18:52:00 +00:00Commented May 22, 2012 at 18:52
Add a comment
|
1 Answer
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.
6 Comments
Marcin
I strongly recommend
requests in all cases.Takkun
I keep getting the error badstatusline caused by the status being empty. Is there any way to ignore this or get the responding XML?
mata
that means your server probably doesn't return a valid http status.
Takkun
why is that a problem? Is there any other module besides httplib for sending http requests?
mata
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. |