【问题标题】:mapping a curl command to equal python requests function将 curl 命令映射到等于 python 请求函数
【发布时间】:2018-12-03 01:05:20
【问题描述】:

真的很感激能帮助我理解我所缺少的东西。 我有一个可用的 curl 命令,我需要使用请求映射到一个相等的 python 函数。

我所有的尝试都以失败告终:
requests.exceptions.ConnectionError: ('Connection aborted.', error(32, 'Broken pipe'))

原来的curl命令是:
curl -X POST "https://x.x.x.x/api/upload" -H "接受:应用程序/json" -H“授权:Basic BlaBla”-H“内容类型:multipart/form-data” -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 只是我用来验证和保留会话以进行后续呼叫的类。我可以使用 fAuth 实例来做没有问题,所以我怀疑问题与底层会话无关。

非常感谢任何想法或见解。

【问题讨论】:

    标签: python python-requests python-requests-html


    【解决方案1】:
    1. -H "accept: application/json" -H "Content-Type: multipart/form-data" 等于 python 中的{"Accept": "application/json"}Content-Type 不是必需的,因为库会添加它。
    2. -H "authorization: Basic BlaBla" 在标题中有点特殊。所以你应该阅读Authentication

    所以你的工作是添加身份验证参数,并通过你的命令示例修改你的标题。

    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)
    

    【讨论】:

      猜你喜欢
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 2018-10-21
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多