用于将文件上传到 REST URL 的 Python 3 脚本(多部分请求)

发布于 2024-12-14 07:54:48 字数 298 浏览 4 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜未央樱花落 2024-12-21 07:54:48

请求库就是您所需要的。您可以使用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)

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)
有深☉意 2024-12-21 07:54:48

如果您知道图像 url 是什么,则上传图像的 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 包含如何上传文件的示例multipart/form-data 具有基本的 http 身份验证。它支持 Python 2.x 和 Python 3。

您还可以使用 < code>请求将文件作为multipart/form-data发布:

#!/usr/bin/env python3
import requests

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

A RESTful way to upload an image would be to use PUT request if you know what the image url is:

#!/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 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 a multipart/form-data:

#!/usr/bin/env python3
import requests

response = requests.post('http://httpbin.org/post',
                         files={'file': open('filename','rb')})
print(response.content)
离笑几人歌 2024-12-21 07:54:48

您还可以使用 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 了解更多信息细节

You can also use unirest . Sample code

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()

You can check out this post http://stackandqueue.com/?p=57 for more details

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文