真的很感激一些帮助来理解我所缺少的。 我有一个有效的 curl 命令,我需要使用 requests 将其映射到相同的 python 函数。
我所有的尝试都以失败告终:
requests.exceptions.ConnectionError: ('Connection aborted.', error(32, 'Broken pipe'))
原来的curl命令是:
curl -X POST "https://x.x.x.x/api/upload "-H "accept: application/json" -H“授权:Basic BlaBla”-H“内容类型:多部分/表单数据” -F "file=@SomeFile.zip;type=application/zip"
我失败的 python 代码是:
def importZip(self, fAuth, infile, dir=''):
trgtURL = fAuth.url+"/api/upload"
headers = {'Content-Type': 'multipart/form-data'}
data = {'upload':''}
files = {'file': (infile, open(dir+infile,'rb'))}
r = fAuth.session.post(trgtURL, files=files, data=data)
fAuth 只是我用来验证和保留后续调用 session 的类。我可以使用 fAuth 实例做 gets 没问题,所以我怀疑问题与底层 session 无关。
非常感谢任何想法或见解。
请您参考如下方法:
-
-H "accept: application/json" -H "Content-Type: multipart/form-data"等于{"Accept": "application/json"}在 python 。Content-Type不是必需的,因为图书馆会添加它。 -
-H "authorization: Basic BlaBla"header 有点特殊。所以你应该阅读 Authentication
所以你的工作是添加 auth 参数,并通过你的命令示例修改你的 header 。
def importZip(self, fAuth, infile, dir=''):
trgtURL = fAuth.url+"/api/upload"
headers = {"Accept": "application/json"}
files = {'file': (infile, open(dir+infile,'rb'), "application/zip")}
auth = (user,passwd)
r = fAuth.session.post(trgtURL, headers=headers, files=files, auth=auth)
