推荐学习书目
› 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
wico77
V2EX  ›  Python

如何使用 requests 发送自定义的 cookies

  •  
  •   wico77 · Feb 15, 2015 · 3496 views
    This topic created in 4243 days ago, the information mentioned may be changed or developed.

    发现在本机和VPS上使用requests获得的信息有些不同。已经自定义了user-agent和referer,可能还有一个差别就是cookies,请问如何使用requests发送自定的cookies。
    谢谢

    6 replies  •  2015-03-02 10:07:15 +08:00
    realityone
        1
    realityone  
       Feb 15, 2015
    help(requests.get)
    help(requests.Request)

    s=requests.Session()
    s.get(url='http://url.url', cookies={'maid': 436, 'mgid': 274, ...... })
    Sylv
        2
    Sylv  
       Feb 15, 2015 via iPhone
    wico77
        3
    wico77  
    OP
       Feb 15, 2015
    @realityone 请问我在VPS上第一次获得的视频播放地址可以正常播放,但第二次就不行了。但在本机上一直没问题。请问问题可能出在哪里?
    tidewind
        4
    tidewind  
       Feb 16, 2015
    session = requests.session()
    user_info = {'username':"[email protected]",'password':'password'}
    r = session.post(url = "http://127.0.0.1:8000/login", data=user_info)
    cookies = dict(r.cookies)

    rep = requests.get(url = "http://127.0.0.1:8000/get_somthing",cookies=cookies)
    janxin
        5
    janxin  
       Feb 16, 2015
    @wico77 第二次可能cookies变掉了,用4#的那个session方法会自动处理cookies。
    tolerious
        6
    tolerious  
       Mar 2, 2015 via iPhone
    @janxin 估计也得要考虑session过期,对否?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3878 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 05:33 · PVG 13:33 · LAX 22:33 · JFK 01:33
    ♥ Do have faith in what you're doing.