将文件上载到RESTURL的Python 3脚本(多部分请求)

将文件上载到RESTURL的Python 3脚本(多部分请求),python,rest,python-3.x,multipart,Python,Rest,Python 3.x,Multipart,我对Python和使用Python 3.2相当陌生。我正在尝试编写一个python脚本,该脚本将从用户机器中选择一个文件(例如图像文件),并使用基于REST的调用将其提交给服务器。Python脚本应该调用RESTURL,并在调用脚本时提交文件 这类似于上传文件时由浏览器完成的多部分POST;但在这里,我想通过Python脚本来实现 如果可能的话,我不希望向Python添加任何外部库,并且希望使用核心Python安装使其保持相当简单的Python脚本 有人能指引我吗?或者分享一些实现我想要的脚本示

我对Python和使用Python 3.2相当陌生。我正在尝试编写一个python脚本,该脚本将从用户机器中选择一个文件(例如图像文件),并使用基于REST的调用将其提交给服务器。Python脚本应该调用RESTURL,并在调用脚本时提交文件

这类似于上传文件时由浏览器完成的多部分POST;但在这里,我想通过Python脚本来实现

如果可能的话,我不希望向Python添加任何外部库,并且希望使用核心Python安装使其保持相当简单的Python脚本


有人能指引我吗?或者分享一些实现我想要的脚本示例?

如果您知道图像url是什么,则可以使用
PUT
request来上传图像:

#!/usr/bin/env python3
import http.client 

h = http.client.HTTPConnection('example.com')
h.request('PUT', '/file/pic.jpg', open('pic.jpg', 'rb'))
print(h.getresponse().read())
包含如何使用基本http身份验证将文件作为
多部分/表单数据上传的示例。它同时支持Python2.x和Python3

您还可以使用将文件作为
多部分/表单数据发布:

#!/usr/bin/env python3
import requests

response = requests.post('http://httpbin.org/post',
                         files={'file': open('filename','rb')})
print(response.content)

请求库是您所需要的。您可以使用
pip安装请求进行安装


您也可以使用unirest。示例代码

import unirest

# consume async post request
def consumePOSTRequestSync():
 params = {'test1':'param1','test2':'param2'}

 # we need to pass a dummy variable which is open method
 # actually unirest does not provide variable to shift between
 # application-x-www-form-urlencoded and
 # multipart/form-data
 params['dummy'] = open('dummy.txt', 'r')
 url = 'http://httpbin.org/post'
 headers = {"Accept": "application/json"}
 # call get service with headers and params
 response = unirest.post(url, headers = headers,params = params)
 print "code:"+ str(response.code)
 print "******************"
 print "headers:"+ str(response.headers)
 print "******************"
 print "body:"+ str(response.body)
 print "******************"
 print "raw_body:"+ str(response.raw_body)

# post sync request multipart/form-data
consumePOSTRequestSync()

你可以查看这篇文章以了解更多细节

@J.F.Sebastian:我已经试过了,但没能成功。正如我所说,我是Python新手,有Java背景。我意识到我可能没有使用所需的正确库。我会试试你的建议。请求现在支持Python3。3.2没有具体原因。作为Python新手,我决定获得最新版本,但没有考虑您提到的方面。我以前见过MultipartPostHandler,但它不起作用,因为我无法安装.egg文件。在3.2中有不同的安装方法吗?无论如何让我试试2.7。你应该等一个端口。这取决于urllib2,urllib2(恕我直言,无法证实!)与Python3不向前兼容。尽管这两篇文章都很有用,但这更接近我的要求。感谢您的帮助。请求现在支持Python3。谢谢,它对单个文件有效。多个文件怎么样?请求现在支持Python3。这个解决方案仅适用于Python3吗?我正在开发Python2.7,会让您知道的@tilaprimera:这个问题有标签,因此答案是针对Python 3的,但几乎相同的代码也适用于Python 2(只需在第一个代码示例中更改导入)。如果我执行请求,是否以相同的方式执行。放在mp3文件上?@tilaprimera:是的。如果您的服务器支持,您也可以使用
内容类型
(这样您就不会使用图像的url来放置mp3文件)。
import unirest

# consume async post request
def consumePOSTRequestSync():
 params = {'test1':'param1','test2':'param2'}

 # we need to pass a dummy variable which is open method
 # actually unirest does not provide variable to shift between
 # application-x-www-form-urlencoded and
 # multipart/form-data
 params['dummy'] = open('dummy.txt', 'r')
 url = 'http://httpbin.org/post'
 headers = {"Accept": "application/json"}
 # call get service with headers and params
 response = unirest.post(url, headers = headers,params = params)
 print "code:"+ str(response.code)
 print "******************"
 print "headers:"+ str(response.headers)
 print "******************"
 print "body:"+ str(response.body)
 print "******************"
 print "raw_body:"+ str(response.raw_body)

# post sync request multipart/form-data
consumePOSTRequestSync()