Python 请求 - 上传 Zip 文件

sdg*_*dgd 3 python curl python-requests

我有一个 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>'))
Run Code Online (Sandbox Code Playgroud)

2:

import requests
fileobj = open('/log/system/check/index.zip', 'rb')
r = requests.post(url, auth=('<username>', '<password>'), files={"archive": ("index.zip", fileobj)})
Run Code Online (Sandbox Code Playgroud)

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

小智 6

也许这会对你有帮助。

 with open(zipname, 'rb') as f:
uploadbase = requests.put('url',
                    auth=(base, pwd),
                    data=f,
                    headers={'X-File-Name' : zipname, 
                                  'Content-Disposition': 'form-data; name="{0}"; filename="{0}"'.format(zipname), 
                                   'content-type': 'multipart/form-data'})
Run Code Online (Sandbox Code Playgroud)

put 和 post 之间的区别