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

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

  •  
  •   wico77 · 2015-02-15 19:17:10 +08:00 · 2665 次点击
    这是一个创建于 3369 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    6 条回复    2015-03-02 10:07:15 +08:00
    realityone
        1
    realityone  
       2015-02-15 19:25:39 +08:00
    help(requests.get)
    help(requests.Request)

    s=requests.Session()
    s.get(url='http://url.url', cookies={'maid': 436, 'mgid': 274, ...... })
    Sylv
        2
    Sylv  
       2015-02-15 20:28:10 +08:00 via iPhone
    wico77
        3
    wico77  
    OP
       2015-02-15 20:52:38 +08:00
    @realityone 请问我在VPS上第一次获得的视频播放地址可以正常播放,但第二次就不行了。但在本机上一直没问题。请问问题可能出在哪里?
    tidewind
        4
    tidewind  
       2015-02-16 14:24:45 +08:00
    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  
       2015-02-16 14:56:10 +08:00
    @wico77 第二次可能cookies变掉了,用4#的那个session方法会自动处理cookies。
    tolerious
        6
    tolerious  
       2015-03-02 10:07:15 +08:00 via iPhone
    @janxin 估计也得要考虑session过期,对否?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2794 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:48 · PVG 23:48 · LAX 08:48 · JFK 11:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.