Python请求-上传一个Zip文件

时间:2018-12-11 14:42:15

标签: python curl python-requests

我有一个需要上传的zip文件。当我使用CURL命令时,它正在上载它,但是当我使用Python请求进行尝试时,我得到了 `Sub PrintSelectionToPDF() Dim ThisRng As Range Dim strfile As String Dim myfile As Variant If Selection.Count = 1 Then Set ThisRng = Application.InputBox("Select a range", "Get Range", Type:=8) Else Set ThisRng = Selection End If 'Prompt for save location strfile = "Selection" & "_" _ & Format(Now(), "yyyymmdd_hhmmss") _ & ".pdf" strfile = ThisWorkbook.Path & "\" & strfile myfile = Application.GetSaveAsFilename _ (InitialFileName:=strfile, _ FileFilter:="PDF Files (*.pdf), *.pdf", _ Title:="Select Folder and File Name to Save as PDF") If myfile <> "False" Then 'save as PDF ThisRng.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ myfile, Quality:=xlQualityStandard, IncludeDocProperties:=True, _ IgnorePrintAreas:=False, OpenAfterPublish:=True Else MsgBox "No File Selected. PDF will not be saved", vbOKOnly, "No File Selected" End If End Sub` 。 压缩文件通常约为500kb。

卷曲命令-

HTTP 405 Method Not Allowed

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

1:

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

2:

import requests
files = {'file': open('/log/system/check/index.zip', 'rb')}
r = requests.post(url, files=files, auth=('<username>', '<password>'))

我缺少明显的东西吗?

2 个答案:

答案 0 :(得分:1)

curl -T ...使用PUT方法而不是POST。 如错误消息所述,您应该使用

r = requests.put(url, ...)

答案 1 :(得分:1)

也许这会对您有所帮助。

 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'})

the difference between put and post