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

为何 urllib2 处理 302 跳转后不带 cookie 呢?

  •  
  •   garth ·
    imgarth · 2014-12-29 12:25:43 +08:00 · 6494 次点击
    这是一个创建于 3378 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在模拟登录时,经常遇到302,偏偏urllib2不继续带着cookie,我就是想让它带着cookie啊。
    14 条回复    2015-01-06 16:36:35 +08:00
    Septembers
        1
    Septembers  
       2014-12-29 12:51:34 +08:00
    requests
    winkidney
        2
    winkidney  
       2014-12-29 12:54:28 +08:00
    使用cookie_jar build_opener
    pc10201
        3
    pc10201  
       2014-12-29 13:03:04 +08:00
    requests也不带~
    zwzmzd
        4
    zwzmzd  
       2014-12-29 13:10:46 +08:00
    试试下面的代码,就是创建一个带cookie处理器的opener
    # 用于Cookies维持
    cookies = urllib2.HTTPCookieProcessor()
    opener = urllib2.build_opener(cookies)

    # 发送一个post请求,注意提交数据的编码encode位置
    data = [\
    ('methord', 'CheckIsHightLeader'),\
    ('leaderNae', LEADER.encode('utf-8'))\
    ]
    request = urllib2.Request(\
    url = url,\
    data = urllib.urlencode(data)\
    )
    opener.open(request, timeout = 10).read()
    garth
        5
    garth  
    OP
       2014-12-29 14:18:52 +08:00
    @Septembers
    @winkidney
    @pc10201
    @zwzmzd
    感谢各位回复。我是使用了opener,但是遇到302跳转时,不会将cookie带到302跳转之后的页面。
    mhycy
        6
    mhycy  
       2014-12-29 14:24:51 +08:00
    那么为了避开这个BUG每次都手工跳转好了...
    garth
        7
    garth  
    OP
       2014-12-29 14:31:36 +08:00
    @mhycy come on 像登录这些常是302跳转的,要是解决这个问题,可以省很多步骤嘛
    GeekGao
        8
    GeekGao  
       2014-12-29 15:39:05 +08:00
    class HTTPRedirectHandler(urllib2.HTTPRedirectHandler):

    def http_error_302(self, req, fp, code, msg, headers):
    return urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)

    jar = cookielib.MozillaCookieJar('/tmp/sina_cookie_output.txt')
    opener = urllib2.build_opener(HTTPRedirectHandler, urllib2.HTTPCookieProcessor(jar))
    garth
        10
    garth  
    OP
       2014-12-29 21:25:01 +08:00
    ahcat
        11
    ahcat  
       2014-12-29 21:58:29 +08:00
    Requests貌似是带的吧?
    http://cn.python-requests.org/zh_CN/latest/user/advanced.html#id2

    会话对象让你能够跨请求保持某些参数。它也会在同一个Session实例发出的所有请求之间保持cookies。

    s = requests.Session()

    s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
    r = s.get("http://httpbin.org/cookies")

    print(r.text)
    # '{"cookies": {"sessioncookie": "123456789"}}'
    mengskysama
        12
    mengskysama  
       2014-12-30 10:02:44 +08:00
    我记得requests也有类似问题,在301中set-cookie不会生效,必须手动指定redirects false手动处理这条cookie。
    winkidney
        13
    winkidney  
       2014-12-30 12:08:38 +08:00
    域不同的话,cookie不能同步的。
    latyas
        14
    latyas  
       2015-01-06 16:36:35 +08:00
    昨晚刚看了一下《HTTP权威指南》,大概是对于跨域了并且服务器返回的set-cookies里没有允许新域访问原域的cookies的话,肯定在新域里是不能带原域的cookies的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2487 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:47 · PVG 23:47 · LAX 08:47 · JFK 11:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.