通过Python中的HTTP请求发送XML的最简单方法? [英] Easiest way to send XML via HTTP requests in Python?
本文介绍了通过Python中的HTTP请求发送XML的最简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有首选的python模块可以帮助我通过HTTP请求发送XML并能够解析返回的XML?
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?
推荐答案
一种方法是使用 urllib2
:
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()
请注意,您必须设置内容类型标头,否则请求将被发送application/x-www-form-urlencoded
.
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.
对于解析响应 lxml
是一个很棒的库,但是 elementtree
也可以.
For parsing the response lxml
is a great library, but elementtree
will also do.
这篇关于通过Python中的HTTP请求发送XML的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文