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

用 Requests 出了一些问题,求大侠指导,谢谢

  •  
  •   kchum · 2014-07-06 03:40:06 +08:00 · 2804 次点击
    这是一个创建于 3586 天前的主题,其中的信息可能已经有所发展或是发生改变。
    A:登陆url B:会员中心(登陆访问) C:申请页面(需登陆访问)

    1.登陆A
    s = requests.Session()
    data = {'username': 'value1', 'password': 'value2'}
    r = s.post("http://example.com/login", data = data)

    2.请求B
    r = s.get("http://example.com/member")
    r.text
    里有username了,说明登陆成功了吧

    3.请求C
    r = s.get("http://example.com/apply")
    r.text
    里提示
    a).your session has timed-out.
    b).you don't have 'cookies' enabled on your browser.
    session都是用同一个对象去请求的,应该不是这个原因吧

    headers,抓包信息排查过了
    那么是不是基本下定论是cookies的原因?
    我看到页面有看到用js操作cookies的,是不是这个原因?
    Python里有什么简单快速的方案解决这个问题,能获取js生成到cookies?

    求大侠指点下,非常感谢!
    第 1 条附言  ·  2014-07-06 07:47:23 +08:00
    抓包和运行详情详情如下

    1.登陆A
    s = requests.Session()
    data = {'username': 'value1', 'password': 'value2'}
    r = s.post("http://example.com/login", data = data)
    ###抓包分析Set-Cookie Auth=xxxxxx , Code = xxxxxxxx ,Python运行一样

    2.请求B
    r = s.get("http://example.com/member")
    r.text
    ###r.text里有username了,说明登陆成功了吧
    ###抓包分析cookies与A的一致,Python运行也一致

    3.请求C
    r = s.get("http://example.com/apply")
    r.text
    ###抓包分析请求C Set-Cookie History=xxxxx , Code = xxxxxxxxx
    ###Python运行获取不到History和新 Code

    真纠结~~~
    5 条回复    2014-07-06 11:22:38 +08:00
    sandtears
        1
    sandtears  
       2014-07-06 04:53:26 +08:00
    读一下 JS 源码照着生成一下呗,反正 js 这种东西都是公开的
    kchum
        2
    kchum  
    OP
       2014-07-06 07:39:43 +08:00
    @sandtears 重新抓包看了下各个url请求header cookie信息和Python的对比,name数是一样的。
    应该跟Js无关。

    抓包流程
    登陆A时setcookie Auth=xxxxxx
    Code = xxxxxxxx

    请求B,还是A的Cookies

    请求C,又setcookie History=xxxxx
    Code = xxxxxxxxx

    Python在C这一步获取不到C设置的cookie(也就是C页面没设置成功History,新Code)

    请问,有哪些情况会导致这种情况出现?

    谢谢~~
    ushuz
        3
    ushuz  
       2014-07-06 09:13:48 +08:00
    把headers设置成和浏览器一模一样试试,可能在apply那一步加了一些对headers的验证。
    sandtears
        4
    sandtears  
       2014-07-06 10:09:25 +08:00
    @kchum 没看明白你的意思,你请求 C 不是设置了新的 Cookie -> History 么, name 数怎么会一样
    jsonline
        5
    jsonline  
       2014-07-06 11:22:38 +08:00 via Android
    cookie过期时间多久?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   951 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:09 · PVG 04:09 · LAX 13:09 · JFK 16:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.