Python等效于curl,用于从文件发布数据

Python等效于curl,用于从文件发布数据,python,curl,Python,Curl,我正在为下面的curl命令寻找一个Python等价物 curlhttp://localhost/x/y/update -H'内容类型:文本/xml;charset=utf-8'--数据二进制@filename.xml 顺便说一句,我通常使用下面的代码将数据作为字符串发布 curlhttp://localhost/x/y/update --数据“数据在此”-H“内容类型:text/xml;字符集=utf-8' baseurl = http://localhost/x/y thedata = '&l

我正在为下面的curl命令寻找一个Python等价物

curlhttp://localhost/x/y/update -H'内容类型:文本/xml;charset=utf-8'--数据二进制@filename.xml

顺便说一句,我通常使用下面的代码将数据作为字符串发布

curlhttp://localhost/x/y/update --数据“数据在此”-H“内容类型:text/xml;字符集=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)
baseurl=http://localhost/x/y
数据='数据在这里'
headers={“内容类型”:“text/xml”,“字符集”:“utf-8”}
thequery=urlparse.urljoin(baseurl、thedata、querycontext)
请求(查询,标题)
响应=urlib2.urlopen(请求)

查看名为

它的应用非常广泛,因此有很多例子说明如何在互联网上使用图书馆


如果您刚刚开始,那么它可能值得一看

使用名为Requests的漂亮python模块 它可以完成90%的curl选项,而且不需要在Windows上编译也可以

与curl、urrlib、urrlib2或httplib、httplib2相比,它非常简单……)

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)
导入请求
headers={'content-type':'text/xml;charset=utf-8'}
response=requests.post(url,data=“数据在这里”,headers=headers)
将open(“filename.xml”、“w”)作为fd:
fd.write(response.text)
pycurl和其他一些用于python的url和http客户端库的问题在于,它需要比实现相对简单的东西所需要的更多的工作。它的方式更加用户友好,我认为这是你在这个问题上寻找的


希望这有帮助

问题是关于上传文件,而公认的答案是保存到文件

下面是正确的代码:

import requests

 # Here you set the file you want to upload and it's content type 
files = {'upload_file': ('filename.xml', open('filename.xml','rb'), 'text/xml' }

headers = {} # Do not set content type here, let the library do its job

response = requests.post(url, 
              data="<data>the data is here</data>", 
              files=files,
              headers=headers)
fd = open("output-response.txt", "w")
fd.write(response.text)
fd.close()
导入请求
#在这里,您可以设置要上载的文件及其内容类型
files={'upload_file':('filename.xml',open('filename.xml','rb'),'text/xml'}
headers={}#不要在这里设置内容类型,让库完成它的工作
response=requests.post(url,
data=“数据在这里”,
文件=文件,
页眉=页眉)
fd=打开(“output response.txt”、“w”)
fd.write(response.text)
fd.close()

上面的代码将从
filename.xml
读取一个文件并通过POST上传,然后它还将接收到的响应存储到
output response.txt
中。

为什么不只是使用请求?@andrefsp谢谢你,但是,我似乎无法准确地确定如何指定我的数据来自一个外部文件。我专门处理如下内容:
urlpath/update?commit=true&tr=x.xsl“-H”内容类型:text/xml;charset=utf-8”-data binary@filename.xml
。如何告诉
请求发布我的有效负载(
{commit':'true',tr:'x.xsl'}
)还有
--data binary@filename.xml
?@phiri您到底想发布什么?{'commit':'true','tr':'x.xsl'}作为json对象?我不确定我是否理解您的问题。@andrefsp我试图发布一个xml编码的文件(在本例中为filename.xml)到Solr服务器进行索引。但是,
UpdateRequestHandler
API有其他URL参数(提交和tr)。我基本上需要知道如何使用
请求
模块处理curl的
--数据二进制文件。@andrefsp非常感谢。所以将
提交和tr
连接到URL(
url=http://localhost:8983/solr/w5/update?commit=true&tr=x.xsl
)然后将文件的内容提供给requests.post的数据参数似乎有效(
payload=open('file.xml','rb')。read()
)问题是关于发送二进制数据,这个答案根本没有给出答案。