我正在使用Python 3.6.2中的HTTP.client与API进行通信。如何使用Python http.client PUT方法上传二进制/视频文件?
为了上传文件,它需要三个阶段的过程。
我已成功地使用POST方法进行通话,并且服务器按照我的预期返回数据。
但是,需要上传实际文件的阶段是PUT方法 - 我无法弄清楚如何语法代码以包含指向存储器上实际文件的指针 - 该文件是mp4视频文件。 这里是代码的我的小白注解:)
#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错误的请求 - 无法获取文件信息。”
我确定这涉及到body =“C:\ Test.mp4”部分的代码。
你能告诉我如何正确引用PUT方法中的文件吗?
在此先感谢
Ledge。像魅力一样工作,谢谢! – yekootmada