【问题标题】:Python 3 script to upload a file to a REST URL (multipart request)将文件上传到 REST URL(多部分请求)的 Python 3 脚本
【发布时间】:2011-12-25 02:17:40
【问题描述】:

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

这类似于上传文件时由浏览器完成的multipart POST;但是这里我想通过 Python 脚本来实现。

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

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

【问题讨论】:

  • @J.F.Sebastian :我已经尝试过link,但无法成功。正如我所说,我是 Python 的新手,来自 Java 背景。我意识到我可能没有使用所需的正确库。我会尝试你的建议。
  • 请求现在支持 Python 3。

标签: python rest python-3.x multipart


【解决方案1】:

Requests 库是您所需要的。您可以使用pip install requests 安装。

http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file

>>> url = 'http://httpbin.org/post'
>>> files = {'file': open('report.xls', 'rb')}
>>> r = requests.post(url, files=files)

【讨论】:

  • 3.2 没有具体原因。作为 Python 新手,我决定获取最新版本,并没有考虑您提到的方面。我以前见过 MultipartPostHandler 但它不起作用,因为我无法安装 .egg 文件。在 3.2 中安装 .egg 有不同的方法吗?无论如何...让我试试 2.7。
  • "install .egg in 3.2" 你应该等待一个端口。它取决于 urllib2 哪个(AFAIK,无法证实这一点!)与 Python 3 不向前兼容。
  • 虽然这两个帖子都有帮助,但这更接近我的要求。感谢您的帮助。
  • 请求现在支持 Python 3。
  • 谢谢,它适用于单个文件。多个文件怎么样?
【解决方案2】:

如果您知道图片的网址是什么,那么上传图片的 RESTful 方式是使用 PUT 请求:

#!/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())

upload_docs.py 包含一个示例,如何使用基本的 http 身份验证将文件上传为 multipart/form-data。它同时支持 Python 2.x 和 Python 3。

您也可以使用requests 将文件作为multipart/form-data 发布:

#!/usr/bin/env python3
import requests

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

【讨论】:

  • 请求现在支持 Python 3。
  • 此解决方案是否仅适用于 python 3?我正在研究 python 2.7,会告诉你的!
  • @tilaprimera:这个问题有python-3.x 标签,因此答案适用于 Python 3,但几乎相同的代码也适用于 Python 2(只需更改第一个代码示例中的导入)。
  • 如果我在一个 mp3 文件上做 request.PUT 是不是也一样?
  • @tilaprimera:是的。如果您的服务器支持,您也可以使用Content-Type(这样您就不会使用图像的 url 放置 mp3 文件)。
【解决方案3】:

您也可以使用 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()

您可以查看此帖子http://stackandqueue.com/?p=57了解更多详情

【讨论】:

    猜你喜欢
    • 2019-12-31
    • 2017-02-11
    • 2021-12-27
    • 2020-07-21
    • 2014-02-04
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    相关资源
    最近更新 更多