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

tornado 的异步请求如何实现模拟登录?

  •  
  •   aiqier · 2017-01-16 20:15:19 +08:00 · 2933 次点击
    这是一个创建于 2654 天前的主题,其中的信息可能已经有所发展或是发生改变。

    之前写 python 的模拟客户端登录,都是使用 python 的 requests 库。因为这个库有 requests.session(),写模拟登录非常方便。

    但现在我的一个 tornado 异步服务,有一个模拟登录的需求。我需要用异步,所以只能用: tornado.httpclient.AsyncHTTPClient()之类的异步方式,不能用 requests 库。

    那么我应该如何用 tornado 的异步库来实现模拟登录的 session ?

    6 条回复    2018-01-30 16:57:04 +08:00
    abc950309
        1
    abc950309  
       2017-01-17 10:42:03 +08:00
    看一下 trequests 吧,别人已经做好轮子了……
    自己写的话,没记错的话,连 Cookie 都要自己解析……
    aiqier
        2
    aiqier  
    OP
       2017-01-17 10:54:16 +08:00
    @abc950309 我咋没搜到这个东西。
    abc950309
        3
    abc950309  
       2017-01-17 11:15:17 +08:00
    @aiqier 请用 Google ……
    https://github.com/1stvamp/trequests
    可能有点老了,可以根据这个思路 fork 一个。
    tyl
        4
    tyl  
       2017-01-17 16:44:48 +08:00
    requests 的 session 无非就是帮你实现了方便的 cookie 管理,让你下次访问该网站的时候自动在 header 里面加上了它的 cookie , tornado 的每个请求都是异步的,所以感觉可以把每次请求获取到的 cookie 存到缓存(比如 redis )里面,每次进行下一步的请求时就可以先从缓存里面读 cookie 然后更新 header 。
    maze1024
        5
    maze1024  
       2017-01-18 10:24:14 +08:00
    aiohttp 也是异步的
    maodei
        6
    maodei  
       2018-01-30 16:57:04 +08:00
    @maze1024 aiohttp 是 python3.X 的。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3258 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:40 · PVG 20:40 · LAX 05:40 · JFK 08:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.