cookielib模块
2016-01-13 14:10
363 查看
cookielib模块的主要作用是提供可存储cookie的对象,以便于与urllib2模块配合使用来访问Internet资源。例如可以利用本模块 的CookieJar类的对象来捕获cookie并在后续连接请求时重新发送。coiokielib模块用到的对象主要有下面几个:CookieJar、 FileCookieJar、MozillaCookieJar、LWPCookieJar。其中他们的关系如下:
CookieJar
/
FileCookieJar
/ \
MozillaCookieJar LWPCookieJar
1、CookieJar ()
管理HTTP cookie值、存储HTTP请求生成的cookie、向传出的HTTP请求添加cookie的对象。整个cookie都存储在内存中,对CookieJar实例进行垃圾回收后cookie也将丢失。
The CookieJar class stores
HTTP cookies. It extracts cookies from HTTP requests, and returns them in HTTP responses.CookieJar instances
automatically expire contained cookies when necessary. Subclasses are also responsible for storing and retrieving cookies from a file or database.
2、FileCookieJar (filename,delayload=None,policy=None)
创建FileCookieJar实例,检索cookie信息并将cookie存储到文件中。filename是存储cookie的文件名。delayload为True时支持延迟访问访问文件,即只有在需要时才读取文件或在文件中存储数据。
A CookieJar which can
load cookies from, and perhaps save cookies to, a file on disk. Cookies are NOT loaded from the named file until either the load() or revert() method
is called.
3、MozillaCookieJar (filename,delayload=None,policy=None)
创建与Mozilla浏览器cookies.txt兼容的FileCookieJar实例。
A FileCookieJar that
can load from and save cookies to disk in the Mozilla cookies.txt file format (which is also used by the Lynx and Netscape browsers).Also note that cookies saved while Mozilla is running will get clobbered by Mozilla.
4、LWPCookieJar (filename,delayload=None,policy=None)
创建与libwww-perl的Set-Cookie3文件格式兼容的FileCookieJar实例。
A FileCookieJar that
can load from and save cookies to disk in format compatible with the libwww-perl library’s Set-Cookie3file format. This is convenient if you want to store cookies in a human-readable file.
除了上面几个函数之外,下面几个函数也很重要:
FileCookieJar. save ( filename=None , ignore_discard=False , ignore_expires=False )
Save cookies to a file.This base class raises NotImplementedError.
Subclasses may leave this method unimplemented.filename is the name of file in which to save cookies. If filename is not specified, self.filename is used (whose default is the value passed to the constructor, if any); if self.filename is None, ValueError is
raised. ignore_discard: save even cookies set to be discarded. ignore_expires: save even cookies that have expired.The file is overwritten if it already exists, thus wiping all the cookies it contains. Saved cookies can be restored later
using the load() or revert() methods.
FileCookieJar. load ( filename=None , ignore_discard=False , ignore_expires=False )
Load cookies from a file.Old cookies are kept unless overwritten by newly loaded ones.Arguments are as for save().The
named file must be in the format understood by the class, or LoadError will
be raised. Also, IOError may
be raised, for example if the file does not exist.
FileCookieJar. revert ( filename=None , ignore_discard=False , ignore_expires=False )
Clear all cookies and reload cookies from a saved file. revert() can
raise the same exceptions as load().
If there is a failure, the object’s state will not be altered.
CookieJar
/
FileCookieJar
/ \
MozillaCookieJar LWPCookieJar
1、CookieJar ()
管理HTTP cookie值、存储HTTP请求生成的cookie、向传出的HTTP请求添加cookie的对象。整个cookie都存储在内存中,对CookieJar实例进行垃圾回收后cookie也将丢失。
The CookieJar class stores
HTTP cookies. It extracts cookies from HTTP requests, and returns them in HTTP responses.CookieJar instances
automatically expire contained cookies when necessary. Subclasses are also responsible for storing and retrieving cookies from a file or database.
2、FileCookieJar (filename,delayload=None,policy=None)
创建FileCookieJar实例,检索cookie信息并将cookie存储到文件中。filename是存储cookie的文件名。delayload为True时支持延迟访问访问文件,即只有在需要时才读取文件或在文件中存储数据。
A CookieJar which can
load cookies from, and perhaps save cookies to, a file on disk. Cookies are NOT loaded from the named file until either the load() or revert() method
is called.
3、MozillaCookieJar (filename,delayload=None,policy=None)
创建与Mozilla浏览器cookies.txt兼容的FileCookieJar实例。
A FileCookieJar that
can load from and save cookies to disk in the Mozilla cookies.txt file format (which is also used by the Lynx and Netscape browsers).Also note that cookies saved while Mozilla is running will get clobbered by Mozilla.
4、LWPCookieJar (filename,delayload=None,policy=None)
创建与libwww-perl的Set-Cookie3文件格式兼容的FileCookieJar实例。
A FileCookieJar that
can load from and save cookies to disk in format compatible with the libwww-perl library’s Set-Cookie3file format. This is convenient if you want to store cookies in a human-readable file.
除了上面几个函数之外,下面几个函数也很重要:
FileCookieJar. save ( filename=None , ignore_discard=False , ignore_expires=False )
Save cookies to a file.This base class raises NotImplementedError.
Subclasses may leave this method unimplemented.filename is the name of file in which to save cookies. If filename is not specified, self.filename is used (whose default is the value passed to the constructor, if any); if self.filename is None, ValueError is
raised. ignore_discard: save even cookies set to be discarded. ignore_expires: save even cookies that have expired.The file is overwritten if it already exists, thus wiping all the cookies it contains. Saved cookies can be restored later
using the load() or revert() methods.
FileCookieJar. load ( filename=None , ignore_discard=False , ignore_expires=False )
Load cookies from a file.Old cookies are kept unless overwritten by newly loaded ones.Arguments are as for save().The
named file must be in the format understood by the class, or LoadError will
be raised. Also, IOError may
be raised, for example if the file does not exist.
FileCookieJar. revert ( filename=None , ignore_discard=False , ignore_expires=False )
Clear all cookies and reload cookies from a saved file. revert() can
raise the same exceptions as load().
If there is a failure, the object’s state will not be altered.
相关文章推荐
- QT5.4.2静态编译(包含QtWebKit),在VS2013上创建项目,并成功运行
- Spring Quartz定时调度任务配置
- DDR(二)
- FIDO 标准简介
- 【30天学习C++ primer系列】day2:基础拾忆之变量和基本类型(1/2)。-20160111
- selenium处理HTML5的视频播放
- linux下MySQL 5.6源码安装
- cpp题目
- datatable同结构和不同结构的合并
- java:IO流学习小结
- 微信网页二维码授权登陆
- Yii框架中的MVC模型
- Ajax跨域问题解决方案
- C#开发接口——MongoDB
- mybatis入门配置详解及实例下载
- awk & sed ,一个老派系统管理员的基本素养
- 滑动删除ListView
- hbuider 框架分析
- Java IO流分析整理
- 第1章第2节练习题14 判断子序列