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
janstk
V2EX  ›  Python

刚接触 python,有些关于 http 请求的东西搞不定.

  •  
  •   janstk · 2017-01-05 15:01:58 +08:00 · 1812 次点击
    这是一个创建于 2684 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现在 python 哪个请求库支持类似这样的 header ?

    大部分的 header 都是通过 dict 封装的,

    多个重复的 header 没法子传啊。

    
    POST /api/xxx/xxxx HTTP/1.1
    Cust-Header: cookie_line_1
    Cust-Header: cookie_line_2
    Cust-Header: cookie_line_3
    Cust-Header: cookie_line_4
    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    User-Agent: Dalvik/2.1.0 (Linux; U; Android 6.0.1; SM-G9200 Build/MMB29K)
    Host: xxx.xxx.com
    Connection: Keep-Alive
    Accept-Encoding: gzip
    Content-Length: 926
    
    
    6 条回复    2017-01-07 01:13:55 +08:00
    mymusise
        1
    mymusise  
       2017-01-05 22:57:55 +08:00
    推荐 requests

    import request
    headers = {
    "Cust-Header": "cookie_line_1",
    "Cust-Header": "cookie_line_2",
    "Cust-Header": "cookie_line_3",
    "Cust-Header": "cookie_line_4",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 6.0.1; SM-G9200 Build/MMB29K)",
    "Host": "xxx.xxx.com",
    "Connection": "Keep-Alive",
    "Accept-Encoding": "gzip",
    "Content-Length": "926"
    }

    url = ""

    requests.post(url, headers=headers)
    janstk
        2
    janstk  
    OP
       2017-01-05 23:27:14 +08:00
    @mymusise 传入的还是 dict ,会被覆盖掉的
    Contextualist
        3
    Contextualist  
       2017-01-06 17:55:12 +08:00 via iPad   ❤️ 1
    mymusise
        4
    mymusise  
       2017-01-06 22:07:55 +08:00
    @janstk 哦,你是这个意思,不过
    Cust-Header: cookie_line_1
    Cust-Header: cookie_line_2
    Cust-Header: cookie_line_3
    Cust-Header: cookie_line_4
    不等效于
    Cust-Header: cookie_line_1 , cookie_line_2,cookie_line_3,cookie_line_4 么?
    janstk
        5
    janstk  
    OP
       2017-01-07 01:11:26 +08:00
    @mymusise 服务器不知道做了什么脑残的东西,没有按 http 协议走,分开传。
    janstk
        6
    janstk  
    OP
       2017-01-07 01:13:55 +08:00
    @Contextualist 感谢,没想到 header 的 value 还能带换行。哈哈哈哈。受教了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2741 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 11:19 · PVG 19:19 · LAX 04:19 · JFK 07:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.