使用Python 3脚本将文件上传到REST URL(多部分请求) [英] Python 3 script to upload a file to a REST URL (multipart request)

查看:387
本文介绍了使用Python 3脚本将文件上传到REST URL(多部分请求)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am fairly new to Python and using Python 3.2. I am trying to write a python script that will pick a file from user machine (such as an image file) and submit it to a server using REST based invocation. The Python script should invoke a REST URL and submit the file when the script is called.

这类似于上载文件时浏览器执行的多部分POST;但是在这里,我想通过Python脚本来做到这一点.

This is similar to multipart POST that is done by browser when uploading a file; but here I want to do it through Python script.

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

If possible do not want to add any external libraries to Python and would like to keep it fairly simple python script using the core Python install.

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

Can some one guide me? or share some script example that achieve what I want?

推荐答案

您需要的是请求库.您可以使用pip install requests安装.

Requests library is what you need. You can install with 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)

这篇关于使用Python 3脚本将文件上传到REST URL(多部分请求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆