Cookie
index
/usr/lib/python2.1/Cookie.py

Here's a sample session to show how to use this module.
At the moment, this is the only documentation.
 
The Basics
----------
 
Importing is easy..
 
   >>> import Cookie
 
Most of the time you start by creating a cookie.  Cookies come in
three flavors, each with slighly different encoding semanitcs, but
more on that later.
 
   >>> C = Cookie.SimpleCookie()
   >>> C = Cookie.SerialCookie()
   >>> C = Cookie.SmartCookie()
 
[Note: Long-time users of Cookie.py will remember using
Cookie.Cookie() to create an Cookie object.  Although deprecated, it
is still supported by the code.  See the Backward Compatibility notes
for more information.]
 
Once you've created your Cookie, you can add values just as if it were
a dictionary.
 
   >>> C = Cookie.SmartCookie()
   >>> C["fig"] = "newton"
   >>> C["sugar"] = "wafer"
   >>> print C
   Set-Cookie: sugar=wafer;
   Set-Cookie: fig=newton;
 
Notice that the printable representation of a Cookie is the
appropriate format for a Set-Cookie: header.  This is the
default behavior.  You can change the header and printed
attributes by using the the .output() function
 
   >>> C = Cookie.SmartCookie()
   >>> C["rocky"] = "road"
   >>> C["rocky"]["path"] = "/cookie"
   >>> print C.output(header="Cookie:")
   Cookie: rocky=road; Path=/cookie;
   >>> print C.output(attrs=[], header="Cookie:")
   Cookie: rocky=road;
 
The load() method of a Cookie extracts cookies from a string.  In a
CGI script, you would use this method to extract the cookies from the
HTTP_COOKIE environment variable.
 
   >>> C = Cookie.SmartCookie()
   >>> C.load("chips=ahoy; vienna=finger")
   >>> print C
   Set-Cookie: vienna=finger;
   Set-Cookie: chips=ahoy;
 
The load() method is darn-tootin smart about identifying cookies
within a string.  Escaped quotation marks, nested semicolons, and other
such trickeries do not confuse it.
 
   >>> C = Cookie.SmartCookie()
   >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
   >>> print C
   Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;";
 
Each element of the Cookie also supports all of the RFC 2109
Cookie attributes.  Here's an example which sets the Path
attribute.
 
   >>> C = Cookie.SmartCookie()
   >>> C["oreo"] = "doublestuff"
   >>> C["oreo"]["path"] = "/"
   >>> print C
   Set-Cookie: oreo=doublestuff; Path=/;
 
Each dictionary element has a 'value' attribute, which gives you
back the value associated with the key.
 
   >>> C = Cookie.SmartCookie()
   >>> C["twix"] = "none for you"
   >>> C["twix"].value
   'none for you'
 
 
A Bit More Advanced
-------------------
 
As mentioned before, there are three different flavors of Cookie
objects, each with different encoding/decoding semantics.  This
section briefly discusses the differences.
 
SimpleCookie
 
The SimpleCookie expects that all values should be standard strings.
Just to be sure, SimpleCookie invokes the str() builtin to convert
the value to a string, when the values are set dictionary-style.
 
   >>> C = Cookie.SimpleCookie()
   >>> C["number"] = 7
   >>> C["string"] = "seven"
   >>> C["number"].value
   '7'
   >>> C["string"].value
   'seven'
   >>> print C
   Set-Cookie: number=7;
   Set-Cookie: string=seven;
 
 
SerialCookie
 
The SerialCookie expects that all values should be serialized using
cPickle (or pickle, if cPickle isn't available).  As a result of
serializing, SerialCookie can save almost any Python object to a
value, and recover the exact same object when the cookie has been
returned.  (SerialCookie can yield some strange-looking cookie
values, however.)
 
   >>> C = Cookie.SerialCookie()
   >>> C["number"] = 7
   >>> C["string"] = "seven"
   >>> C["number"].value
   7
   >>> C["string"].value
   'seven'
   >>> print C
   Set-Cookie: number="I7\012.";
   Set-Cookie: string="S'seven'\012p1\012.";
 
Be warned, however, if SerialCookie cannot de-serialize a value (because
it isn't a valid pickle'd object), IT WILL RAISE AN EXCEPTION.
 
 
SmartCookie
 
The SmartCookie combines aspects of each of the other two flavors.
When setting a value in a dictionary-fashion, the SmartCookie will
serialize (ala cPickle) the value *if and only if* it isn't a
Python string.  String objects are *not* serialized.  Similarly,
when the load() method parses out values, it attempts to de-serialize
the value.  If it fails, then it fallsback to treating the value
as a string.
 
   >>> C = Cookie.SmartCookie()
   >>> C["number"] = 7
   >>> C["string"] = "seven"
   >>> C["number"].value
   7
   >>> C["string"].value
   'seven'
   >>> print C
   Set-Cookie: number="I7\012.";
   Set-Cookie: string=seven;
 
 
Backwards Compatibility
-----------------------
 
In order to keep compatibilty with earlier versions of Cookie.py,
it is still possible to use Cookie.Cookie() to create a Cookie.  In
fact, this simply returns a SmartCookie.
 
   >>> C = Cookie.Cookie()
   >>> print C.__class__.__name__
   SmartCookie
 
 
Finis.

 
Modules
            
re
string
sys

 
Classes
            
exceptions.Exception
CookieError
UserDict.UserDict
BaseCookie
SerialCookie
SimpleCookie
SmartCookie
SmartCookie
Morsel

 
class BaseCookie(UserDict.UserDict)
      # At long last, here is the cookie class.
#   Using this class is almost just like using a dictionary.
# See this module's docstring for example usage.
 
  
_BaseCookie__ParseString = __ParseString(self, str, patt=<SRE_Pattern object>)
_BaseCookie__set = __set(self, key, real_value, coded_value)
Private method for setting a cookie's value
__cmp__(self, dict) from UserDict.UserDict
__delitem__(self, key) from UserDict.UserDict
__getitem__(self, key) from UserDict.UserDict
__init__(self, input=None)
__len__(self) from UserDict.UserDict
__repr__(self)
__setitem__(self, key, value)
Dictionary style assignment.
__str__ = output(self, attrs=None, header='Set-Cookie:', sep='\n')
clear(self) from UserDict.UserDict
copy(self) from UserDict.UserDict
get(self, key, failobj=None) from UserDict.UserDict
has_key(self, key) from UserDict.UserDict
items(self) from UserDict.UserDict
js_output(self, attrs=None)
Return a string suitable for JavaScript.
keys(self) from UserDict.UserDict
load(self, rawdata)
Load cookies from a string (presumably HTTP_COOKIE) or
from a dictionary.  Loading cookies from a dictionary 'd'
is equivalent to calling:
    map(Cookie.__setitem__, d.keys(), d.values())
output(self, attrs=None, header='Set-Cookie:', sep='\n')
Return a string suitable for HTTP.
popitem(self) from UserDict.UserDict
setdefault(self, key, failobj=None) from UserDict.UserDict
update(self, dict) from UserDict.UserDict
value_decode(self, val)
real_value, coded_value = value_decode(STRING)
Called prior to setting a cookie's value from the network
representation.  The VALUE is the value read from HTTP
header.
Override this function to modify the behavior of cookies.
value_encode(self, val)
real_value, coded_value = value_encode(VALUE)
Called prior to setting a cookie's value from the dictionary
representation.  The VALUE is the value being assigned.
Override this function to modify the behavior of cookies.
values(self) from UserDict.UserDict

 
Cookie = class SmartCookie(BaseCookie)
      SmartCookie
SmartCookie supports arbitrary objects as cookie values.  If the
object is a string, then it is quoted.  If the object is not a
string, however, then SmartCookie will use cPickle to serialize
the object into a string representation.
 
Note: Large cookie values add overhead because they must be
retransmitted on every HTTP transaction.
 
Note: HTTP has a 2k limit on the size of a cookie.  This class
does not check for this limit, so be careful!!!
 
  
_BaseCookie__ParseString = __ParseString(self, str, patt=<SRE_Pattern object>) from BaseCookie
_BaseCookie__set = __set(self, key, real_value, coded_value) from BaseCookie
__cmp__(self, dict) from UserDict.UserDict
__delitem__(self, key) from UserDict.UserDict
__getitem__(self, key) from UserDict.UserDict
__init__(self, input=None) from BaseCookie
__len__(self) from UserDict.UserDict
__repr__(self) from BaseCookie
__setitem__(self, key, value) from BaseCookie
__str__ = output(self, attrs=None, header='Set-Cookie:', sep='\n') from BaseCookie
clear(self) from UserDict.UserDict
copy(self) from UserDict.UserDict
get(self, key, failobj=None) from UserDict.UserDict
has_key(self, key) from UserDict.UserDict
items(self) from UserDict.UserDict
js_output(self, attrs=None) from BaseCookie
keys(self) from UserDict.UserDict
load(self, rawdata) from BaseCookie
output(self, attrs=None, header='Set-Cookie:', sep='\n') from BaseCookie
popitem(self) from UserDict.UserDict
setdefault(self, key, failobj=None) from UserDict.UserDict
update(self, dict) from UserDict.UserDict
value_decode(self, val)
value_encode(self, val)
values(self) from UserDict.UserDict

 
class CookieError(exceptions.Exception)
      # Define an exception visible to External modules
 
  
__getitem__(...) from exceptions.Exception
__init__(...) from exceptions.Exception
__str__(...) from exceptions.Exception

 
class Morsel(UserDict.UserDict)
       
  
OutputString(self, attrs=None)
__cmp__(self, dict) from UserDict.UserDict
__delitem__(self, key) from UserDict.UserDict
__getitem__(self, key) from UserDict.UserDict
__init__(self)
__len__(self) from UserDict.UserDict
__repr__(self)
__setitem__(self, K, V)
__str__ = output(self, attrs=None, header='Set-Cookie:')
clear(self) from UserDict.UserDict
copy(self) from UserDict.UserDict
get(self, key, failobj=None) from UserDict.UserDict
has_key(self, key) from UserDict.UserDict
isReservedKey(self, K)
items(self) from UserDict.UserDict
js_output(self, attrs=None)
keys(self) from UserDict.UserDict
output(self, attrs=None, header='Set-Cookie:')
popitem(self) from UserDict.UserDict
set(self, key, val, coded_val, LegalChars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&'*+-.^_`|~", idmap='\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./...\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff', translate=<function translate>)
setdefault(self, key, failobj=None) from UserDict.UserDict
update(self, dict) from UserDict.UserDict
values(self) from UserDict.UserDict

 
class SerialCookie(BaseCookie)
      SerialCookie
SerialCookie supports arbitrary objects as cookie values. All
values are serialized (using cPickle) before being sent to the
client.  All incoming values are assumed to be valid Pickle
representations.  IF AN INCOMING VALUE IS NOT IN A VALID PICKLE
FORMAT, THEN AN EXCEPTION WILL BE RAISED.
 
Note: Large cookie values add overhead because they must be
retransmitted on every HTTP transaction.
 
Note: HTTP has a 2k limit on the size of a cookie.  This class
does not check for this limit, so be careful!!!
 
  
_BaseCookie__ParseString = __ParseString(self, str, patt=<SRE_Pattern object>) from BaseCookie
_BaseCookie__set = __set(self, key, real_value, coded_value) from BaseCookie
__cmp__(self, dict) from UserDict.UserDict
__delitem__(self, key) from UserDict.UserDict
__getitem__(self, key) from UserDict.UserDict
__init__(self, input=None) from BaseCookie
__len__(self) from UserDict.UserDict
__repr__(self) from BaseCookie
__setitem__(self, key, value) from BaseCookie
__str__ = output(self, attrs=None, header='Set-Cookie:', sep='\n') from BaseCookie
clear(self) from UserDict.UserDict
copy(self) from UserDict.UserDict
get(self, key, failobj=None) from UserDict.UserDict
has_key(self, key) from UserDict.UserDict
items(self) from UserDict.UserDict
js_output(self, attrs=None) from BaseCookie
keys(self) from UserDict.UserDict
load(self, rawdata) from BaseCookie
output(self, attrs=None, header='Set-Cookie:', sep='\n') from BaseCookie
popitem(self) from UserDict.UserDict
setdefault(self, key, failobj=None) from UserDict.UserDict
update(self, dict) from UserDict.UserDict
value_decode(self, val)
value_encode(self, val)
values(self) from UserDict.UserDict

 
class SimpleCookie(BaseCookie)
      SimpleCookie
SimpleCookie supports strings as cookie values.  When setting
the value using the dictionary assignment notation, SimpleCookie
calls the builtin str() to convert the value to a string.  Values
received from HTTP are kept as strings.
 
  
_BaseCookie__ParseString = __ParseString(self, str, patt=<SRE_Pattern object>) from BaseCookie
_BaseCookie__set = __set(self, key, real_value, coded_value) from BaseCookie
__cmp__(self, dict) from UserDict.UserDict
__delitem__(self, key) from UserDict.UserDict
__getitem__(self, key) from UserDict.UserDict
__init__(self, input=None) from BaseCookie
__len__(self) from UserDict.UserDict
__repr__(self) from BaseCookie
__setitem__(self, key, value) from BaseCookie
__str__ = output(self, attrs=None, header='Set-Cookie:', sep='\n') from BaseCookie
clear(self) from UserDict.UserDict
copy(self) from UserDict.UserDict
get(self, key, failobj=None) from UserDict.UserDict
has_key(self, key) from UserDict.UserDict
items(self) from UserDict.UserDict
js_output(self, attrs=None) from BaseCookie
keys(self) from UserDict.UserDict
load(self, rawdata) from BaseCookie
output(self, attrs=None, header='Set-Cookie:', sep='\n') from BaseCookie
popitem(self) from UserDict.UserDict
setdefault(self, key, failobj=None) from UserDict.UserDict
update(self, dict) from UserDict.UserDict
value_decode(self, val)
value_encode(self, val)
values(self) from UserDict.UserDict

 
class SmartCookie(BaseCookie)
      SmartCookie
SmartCookie supports arbitrary objects as cookie values.  If the
object is a string, then it is quoted.  If the object is not a
string, however, then SmartCookie will use cPickle to serialize
the object into a string representation.
 
Note: Large cookie values add overhead because they must be
retransmitted on every HTTP transaction.
 
Note: HTTP has a 2k limit on the size of a cookie.  This class
does not check for this limit, so be careful!!!
 
  
_BaseCookie__ParseString = __ParseString(self, str, patt=<SRE_Pattern object>) from BaseCookie
_BaseCookie__set = __set(self, key, real_value, coded_value) from BaseCookie
__cmp__(self, dict) from UserDict.UserDict
__delitem__(self, key) from UserDict.UserDict
__getitem__(self, key) from UserDict.UserDict
__init__(self, input=None) from BaseCookie
__len__(self) from UserDict.UserDict
__repr__(self) from BaseCookie
__setitem__(self, key, value) from BaseCookie
__str__ = output(self, attrs=None, header='Set-Cookie:', sep='\n') from BaseCookie
clear(self) from UserDict.UserDict
copy(self) from UserDict.UserDict
get(self, key, failobj=None) from UserDict.UserDict
has_key(self, key) from UserDict.UserDict
items(self) from UserDict.UserDict
js_output(self, attrs=None) from BaseCookie
keys(self) from UserDict.UserDict
load(self, rawdata) from BaseCookie
output(self, attrs=None, header='Set-Cookie:', sep='\n') from BaseCookie
popitem(self) from UserDict.UserDict
setdefault(self, key, failobj=None) from UserDict.UserDict
update(self, dict) from UserDict.UserDict
value_decode(self, val)
value_encode(self, val)
values(self) from UserDict.UserDict

 
Functions
            
_getdate(future=0, weekdayname=['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], monthname=[None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
_quote(str, LegalChars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&'*+-.^_`|~", join=<function join>, idmap='\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./...\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff', translate=<function translate>)
_test()
_unquote(str, join=<function join>, atoi=<function atoi>)
dumps(...)
dumps(object, [binary]) --Return a string containing an object in pickle format
 
If the optional argument, binary, is provided and is true, then the
pickle will be written in binary format, which is more space and
computationally efficient.
loads(...)
loads(string) -- Load a pickle from the given string

 
Data
             _CookiePattern = <SRE_Pattern object>
_LegalChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&'*+-.^_`|~"
_LegalCharsPatt = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]"
_OctalPatt = <SRE_Pattern object>
_QuotePatt = <SRE_Pattern object>
_Translator = {'\x00': r'\000', '\x01': r'\001', '\x02': r'\002', '\x03': r'\003', '\x04': r'\004', '\x05': r'\005', '\x06': r'\006', '\x07': r'\007', '\x08': r'\010', '\t': r'\011', ...}
__all__ = ['CookieError', 'BaseCookie', 'SimpleCookie', 'SerialCookie', 'SmartCookie', 'Cookie']
__file__ = '/usr/lib/python2.1/Cookie.pyc'
__name__ = 'Cookie'
_monthname = [None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
_weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']