Easiest way to send XML via HTTP requests in Python?

馋奶兔 提交于 2019-12-19 10:19:45

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/10707959/easiest-way-to-send-xml-via-http-requests-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!