如何使用Python http.client PUT方法上传二进制/视频文件? [英] How to upload a binary/video file using Python http.client PUT method?

查看:464
本文介绍了如何使用Python http.client PUT方法上传二进制/视频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 3.6.2中的HTTP.client与API通信.

I am communicating with an API using HTTP.client in Python 3.6.2.

要上传文件,它需要经过三个阶段.

In order to upload a file it requires a three stage process.

我已经成功地使用POST方法进行了对话,并且服务器按预期返回了数据.

I have managed to talk successfully using POST methods and the server returns data as I expect.

但是,需要上传实际文件的阶段是PUT方法-我无法弄清楚如何对代码进行语法处理以在存储中包含指向实际文件的指针-该文件是mp4视频文件. 这是带有我的noob注释的代码片段:)

However, the stage that requires the actual file to be uploaded is a PUT method - and I cannot figure out how to syntax the code to include a pointer to the actual file on my storage - the file is an mp4 video file. Here is a snippet of the code with my noob annotations :)

#define connection as HTTPS and define URL
uploadstep2 = http.client.HTTPSConnection("grabyo-prod.s3-accelerate.amazonaws.com")

#define headers
headers = {
    'accept': "application/json",
    'content-type': "application/x-www-form-urlencoded"
}

#define the structure of the request and send it.
#Here it is a PUT request to the unique URL as defined above with the correct file and headers.
uploadstep2.request("PUT", myUniqueUploadUrl, body="C:\Test.mp4", headers=headers)

#get the response from the server
uploadstep2response = uploadstep2.getresponse()

#read the data from the response and put to a usable variable
step2responsedata = uploadstep2response.read()

我在此阶段得到的答复是 错误400错误的请求-无法获取文件信息."

The response I am getting back at this stage is an "Error 400 Bad Request - Could not obtain the file information."

我确定这与代码的 body ="C:\ Test.mp4" 部分有关.

I am certain this relates to the body="C:\Test.mp4" section of the code.

您能告诉我如何在PUT方法中正确引用文件吗?

Can you please advise how I can correctly reference a file within the PUT method?

预先感谢

推荐答案

uploadstep2.request("PUT", myUniqueUploadUrl, body="C:\Test.mp4", headers=headers)

会将实际的字符串"C:\Test.mp4"放在请求的正文中,而不是您期望的名为"C:\Test.mp4"的文件的内容.

will put the actual string "C:\Test.mp4" in the body of your request, not the content of the file named "C:\Test.mp4" as you expect.

您需要打开文件,读取文件内容,然后将其作为主体传递.或进行流式传输,但AFAIK http.client不支持该流式传输,并且由于您的文件似乎是视频,因此它可能很大,并且会在没有充分理由的情况下使用大量RAM.

You need to open the file, read it's content then pass it as body. Or to stream it, but AFAIK http.client does not support that, and since your file seems to be a video, it is potentially huge and will use plenty of RAM for no good reason.

我的建议是使用 requests ,这是更好的lib进行此类操作的方法的东西:

My suggestion would be to use requests, which is a way better lib to do this kind of things:

import requests
with open(r'C:\Test.mp4'), 'rb') as finput:
   response = requests.put('https://grabyo-prod.s3-accelerate.amazonaws.com/youruploadpath', data=finput)
   print(response.json())

这篇关于如何使用Python http.client PUT方法上传二进制/视频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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