Python请求PUT方法创建一个零字节文件

编程入门 行业动态 更新时间:2024-10-24 20:21:51
本文介绍了Python请求PUT方法创建一个零字节文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用python中的请求模块通过PUT上传文件.

我的代码是这样的:

with open(file, 'rb') as payload: r = requests.put(url, data=payload, auth=('username', 'password'))

文件已创建,我得到200响应,但它有0个字节.如果我没有做错什么,我怀疑我在此处遇到了这个错误./p>

是这种情况吗?如果是,我可以尝试任何解决方法吗?

我也尝试过使用httplib2库

with open(file, 'rb') as payload: h = httplib2.Http(".cache") h.add_credentials('user', 'pass') resp, content = h.request(url, "PUT", body=payload)

但是请求永远保持挂起状态(再次创建大小为0的文件).请求模块也可能是同样的问题吗?

一些额外的信息.

接收PUT的服务正在ESXi虚拟机管理程序上运行.它具有一项功能,如果您发出经过身份验证的PUT请求,它将请求的文件存储在/tmp中.服务器端正在工作(用执行相同工作并且还使用curl的perl脚本对其进行了测试).

上传的文件是一个.tgz文件,位于我的本地文件系统上,URL的格式为"esx-server/tmp/file.tgz".

解决方案

所以看来确实存在某种错误.

我的解决方案是使用urllib2.它不像请求那样干净".但是仍然比我想象的要好.

我现在的工作代码是:

import urllib2 from base64 import b64encode with open(source, 'rb') as file: data = file.read() request = urllib2.Request(url) request.add_data(data) request.add_header('Authorization', 'Basic ' + b64encode(username + ':' + password)) request.get_method = lambda: "PUT" r = urllib2.urlopen(request)

I am trying to upload a file with PUT using the requests module in python.

my code is this:

with open(file, 'rb') as payload: r = requests.put(url, data=payload, auth=('username', 'password'))

The file is created, I am getting a response 200, but it has 0 bytes. If I am not doing something wrong I suspect that I have met the bug here.

Is this the case? If yes is there any workaround that I can try?

I've tried also the same with the httplib2 library

with open(file, 'rb') as payload: h = httplib2.Http(".cache") h.add_credentials('user', 'pass') resp, content = h.request(url, "PUT", body=payload)

But the request stays hanging for ever (again a 0 size file is created). Could it also be the same problem with the requests module?

[EDIT] Some extra info.

The service receiving the PUT is running on a ESXi hypervisor. It has a function where if you make an authed PUT request it stores the file of the request in /tmp. The server side is working (tested it with a perl script which does the same job and also with curl).

The file uploaded is a .tgz file which lies on my local filesystem and the url is in the form of "esx-server/tmp/file.tgz".

解决方案

So it seems that there is some kind of bug indeed.

My solution was to use the urllib2. It isn't as "clean" as requests. But still better than what I thought it would be.

My working code now is:

import urllib2 from base64 import b64encode with open(source, 'rb') as file: data = file.read() request = urllib2.Request(url) request.add_data(data) request.add_header('Authorization', 'Basic ' + b64encode(username + ':' + password)) request.get_method = lambda: "PUT" r = urllib2.urlopen(request)

更多推荐

Python请求PUT方法创建一个零字节文件

本文发布于:2023-11-02 08:08:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551845.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:创建一个   字节   文件   方法   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!