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