用于将文件上传到 REST URL 的 Python 3 脚本(多部分请求)
我对 Python 和使用 Python 3.2 相当陌生。我正在尝试编写一个 python 脚本,该脚本将从用户计算机中选择一个文件(例如图像文件)并使用基于 REST 的调用将其提交到服务器。 Python 脚本应调用 REST URL 并在调用脚本时提交文件。
这类似于浏览器上传文件时完成的多部分 POST;但这里我想通过Python脚本来完成。
如果可能的话,不想向 Python 添加任何外部库,并且希望使用核心 Python 安装保持相当简单的 python 脚本。
有人可以指导我吗?或者分享一些实现我想要的脚本示例?
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.
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请求库就是您所需要的。您可以使用
pip install requests
进行安装。http://docs.python- requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
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 是什么,则上传图像的 RESTful 方法是使用
PUT
请求:upload_docs.py 包含如何上传文件的示例
multipart/form-data
具有基本的 http 身份验证。它支持 Python 2.x 和 Python 3。您还可以使用 < code>请求将文件作为
multipart/form-data
发布:A RESTful way to upload an image would be to use
PUT
request if you know what the image url is:upload_docs.py contains an example how to upload a file as
multipart/form-data
with basic http authentication. It supports both Python 2.x and Python 3.You could use also
requests
to post files as amultipart/form-data
:您还可以使用 unirest 。示例代码
您可以查看这篇文章 http://stackandqueue.com/?p=57 了解更多信息细节
You can also use unirest . Sample code
You can check out this post http://stackandqueue.com/?p=57 for more details