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

Python 中如何禁用 cookies

  •  
  •   sdlearn · Feb 6, 2017 · 4497 views
    This topic created in 3566 days ago, the information mentioned may be changed or developed.

    由于 python 程序中需要登录操作所以在一个函数中通过以下方式启用了 cookies :

    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)
    

    但是登录完成后其他的一些操作不再需要 cookies 了,而且 cookies 越少越好,所以我想再禁用了它,该如何操作呢

    5 replies  •  2017-02-25 15:04:14 +08:00
    xiaket
    xiaket
    Feb 6, 2017
    1
    遇到这种用 urllib/urllib2 的情形,永远推荐 requests.
    XYxe
    XYxe
    Feb 6, 2017 via Android
    2
    别 install_opener 嘛,需要 cookie 的时候用 opemer ,不需要的时候直接用 urllib2.urlopen 。
    用 requests 也差不多,需要的时候用 session ,不需要的时候直接用 requests.get 。
    lightening
    lightening
    Feb 6, 2017
    3
    不知道如何禁用 cookies ,不过你确定登录完成后就不需要 cookie 了么,那么登录状态是如何保存的呢?
    zhtsuc
    zhtsuc
    Feb 7, 2017
    4
    你不写 cookie ,或者请求的时候不发 cookie 不就行了
    sdlearn
    sdlearn
    OP
    Feb 25, 2017
    5
    @lightening 登录上执行完想要的操作(这步需要 cookies )后就不需要登录状态了,后面的操作需要 cookies 越少越好
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2493 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 12:10 · PVG 20:10 · LAX 04:10 · JFK 07:10
    ♥ Do have faith in what you're doing.