V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Zuckonit
V2EX  ›  Python

requests 如何带证书上传文件

  •  
  •   Zuckonit · 2014-07-08 16:58:35 +08:00 · 6355 次点击
    这是一个创建于 3582 天前的主题,其中的信息可能已经有所发展或是发生改变。
    下面的语句如何在python 的 requests库里面实现
    filename=/tmp/testfile
    curl --cacert test.cert -X POST -F "hash=$hash_v" -F "filename=@$filename" "https://0.0.0.0:12345/upload/"

    意思是通过POST方法, body有两个参数, 一个是hash, 一个文件
    第 1 条附言  ·  2014-07-08 19:18:39 +08:00
    google一番, 基本都不对, 拼凑测试几次后通过, 总结如下, 希望对遇到同样问题的人有帮助

    filename = '/tmp/test.cert'
    hash_v = 'assumethisisahash'
    with open(filename, 'rb') as f:
    ....requests.post(link, data={'hash': hash_v}, files={'filename':f}, verify='/tmp/test.cert')
    4 条回复    2014-07-08 19:17:31 +08:00
    yueyoum
        2
    yueyoum  
       2014-07-08 17:24:14 +08:00
    request.post(url, cert="test.cert")
    Zuckonit
        3
    Zuckonit  
    OP
       2014-07-08 18:56:43 +08:00
    @yueyoum 试了报错, @keakon给的link里面也没看到我这种场景的demo, curl可以。能把curl命令翻译成requests不(对了证书我自己生成的)
    Zuckonit
        4
    Zuckonit  
    OP
       2014-07-08 19:17:31 +08:00
    google一番, 基本都不对, 拼凑测试几次后通过, 总结如下, 希望对遇到同样问题的人有帮助

    filename = '/tmp/test.cert'
    hash_v = 'assumethisisahash'
    with open(filename, 'rb') as f:
    ....requests.post(link, data={'hash': hash_v}, files={'filename':f}, verify='/tmp/test.cert')
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1079 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 18:46 · PVG 02:46 · LAX 11:46 · JFK 14:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.