猿问

Python 请求 - 上传 Zip 文件

我有一个需要上传的 zip 文件。当我使用 CURL 命令时,它正在上传它,但是当我使用 Python 请求尝试相同时,我得到HTTP 405 Method Not Allowed. zip 文件通常在 500kb 左右。


卷曲命令 -


curl -u<username>:<password> -T /log/system/check/index.zip "<target URL>"


Python 脚本(尝试了 2 种不同的方式)-


1:


import requests

files = {'file': open('/log/system/check/index.zip', 'rb')}

r = requests.post(url, files=files, auth=('<username>', '<password>'))

2:


import requests

fileobj = open('/log/system/check/index.zip', 'rb')

r = requests.post(url, auth=('<username>', '<password>'), files={"archive": ("index.zip", fileobj)})

我错过了一些明显的东西吗?


Helenr
浏览 560回答 2
2回答

梦里花落0921

可能这会帮助你。&nbsp;with open(zipname, 'rb') as f:uploadbase = requests.put('url',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; auth=(base, pwd),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data=f,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers={'X-File-Name' : zipname,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Content-Disposition': 'form-data; name="{0}"; filename="{0}"'.format(zipname),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'content-type': 'multipart/form-data'})put 和 post 的区别

倚天杖

curl -T ...使用 PUT 方法而不是 POST。正如错误消息所说,您应该使用r = requests.put(url, ...)
随时随地看视频慕课网APP

相关分类

Python
我要回答