【问题标题】:Use python requests to write a curl command with parameters like -u and -T使用 python 请求编写带有 -u 和 -T 等参数的 curl 命令
【发布时间】:2019-01-11 08:28:47
【问题描述】:

我正在尝试复制以下格式的 curl 命令:

curl -X PUT -w "<some string>" \
     -u <user_name>:<password> \
     -T <some_string> \
     <some_url>

我尝试在header中添加以下格式的参数,但没有成功:

url = ''
headers = {'u': '<user>', 'T': '<string>'}

response = requests.post(url, headers=headers)

【问题讨论】:

标签: python python-requests


【解决方案1】:

那些 -u-T 标志不是标题,它们会触发 curl 中的特定行为。你需要understand that behaviour 并在the requests manual 中查找如何做同样的事情。

-X设置请求方式,这里PUT,对应requests.put
-u对应auth=(user, pass) argument
-T上传文件,对应files={...} parameter.

把它们放在一起,你想要这样的东西:

requests.put(url, 
             auth=('username', 'password'),
             files={'file': open('filename', 'rb')})

【讨论】:

    【解决方案2】:
    1. 您将 curl 与 put 一起使用,但您将 requests 与 post 一起使用。

    2. 请阅读请求文档。

    3.response = requests.put('http://aaaa', auth=('', ''),data={},files = {'file': open('report.xls', 'rb' )})

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 2016-02-08
      • 2020-09-17
      • 2019-06-22
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多