我正在为下面的 curl 命令寻找 Python 等效项。
curl http://localhost/x/y/update -H 'Content-type: text/xml; charset=utf-8' --data-binary @filename.xml
顺便说一句,我通常使用下面的代码将数据作为字符串发布。
curl http://localhost/x/y/update --data '<data>the data is here</data>' -H 'Content-type:text/xml; charset=utf-8'
baseurl = http://localhost/x/y
thedata = '<data>the data is here</data>'
headers = {"Content-type": "text/xml", "charset": "utf-8"}
thequery = urlparse.urljoin(baseurl, thedata, querycontext)
therequest = urllib2.Request(thequery, headers)
theresponse = urllib2.urlopen(therequest)
Run Code Online (Sandbox Code Playgroud)
Python要求它有一个很棒的库来处理这类东西。您可以通过以下方式轻松完成:
import requests
headers = {'content-type': 'text/xml; charset=utf-8'}
response = requests.post(url, data="<data>the data is here</data>", headers=headers)
with open("filename.xml", "w") as fd:
fd.write(response.text)
Run Code Online (Sandbox Code Playgroud)
pycurl 和其他一些用于 python 的 url 和 http 客户端库的问题在于,它需要比实现一些相对简单的东西所需的更多努力。要求它的方式更加用户友好,我认为它是您在这个问题上寻找的。
希望这可以帮助
| 归档时间: |
|
| 查看次数: |
7623 次 |
| 最近记录: |