5050
5151 >>> from http import cookies
5252
53- Most of the time you start by creating a cookie. Cookies come in
54- three flavors, each with slightly different encoding semantics, but
55- more on that later.
53+ Most of the time you start by creating a cookie.
5654
5755 >>> C = cookies.SimpleCookie()
58- >>> C = cookies.SerialCookie()
59- >>> C = cookies.SmartCookie()
60-
61- [Note: Long-time users of cookies.py will remember using
62- cookies.Cookie() to create an Cookie object. Although deprecated, it
63- is still supported by the code. See the Backward Compatibility notes
64- for more information.]
6556
6657Once you've created your Cookie, you can add values just as if it were
6758a dictionary.
6859
69- >>> C = cookies.SmartCookie ()
60+ >>> C = cookies.SimpleCookie ()
7061 >>> C["fig"] = "newton"
7162 >>> C["sugar"] = "wafer"
7263 >>> C.output()
7768default behavior. You can change the header and printed
7869attributes by using the .output() function
7970
80- >>> C = cookies.SmartCookie ()
71+ >>> C = cookies.SimpleCookie ()
8172 >>> C["rocky"] = "road"
8273 >>> C["rocky"]["path"] = "/cookie"
8374 >>> print(C.output(header="Cookie:"))
8980CGI script, you would use this method to extract the cookies from the
9081HTTP_COOKIE environment variable.
9182
92- >>> C = cookies.SmartCookie ()
83+ >>> C = cookies.SimpleCookie ()
9384 >>> C.load("chips=ahoy; vienna=finger")
9485 >>> C.output()
9586 'Set-Cookie: chips=ahoy\r\nSet-Cookie: vienna=finger'
9889within a string. Escaped quotation marks, nested semicolons, and other
9990such trickeries do not confuse it.
10091
101- >>> C = cookies.SmartCookie ()
92+ >>> C = cookies.SimpleCookie ()
10293 >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
10394 >>> print(C)
10495 Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
10798Cookie attributes. Here's an example which sets the Path
10899attribute.
109100
110- >>> C = cookies.SmartCookie ()
101+ >>> C = cookies.SimpleCookie ()
111102 >>> C["oreo"] = "doublestuff"
112103 >>> C["oreo"]["path"] = "/"
113104 >>> print(C)
116107Each dictionary element has a 'value' attribute, which gives you
117108back the value associated with the key.
118109
119- >>> C = cookies.SmartCookie ()
110+ >>> C = cookies.SimpleCookie ()
120111 >>> C["twix"] = "none for you"
121112 >>> C["twix"].value
122113 'none for you'
123114
124-
125- A Bit More Advanced
126- -------------------
127-
128- As mentioned before, there are three different flavors of Cookie
129- objects, each with different encoding/decoding semantics. This
130- section briefly discusses the differences.
131-
132- SimpleCookie
133-
134115The SimpleCookie expects that all values should be standard strings.
135116Just to be sure, SimpleCookie invokes the str() builtin to convert
136117the value to a string, when the values are set dictionary-style.
145126 >>> C.output()
146127 'Set-Cookie: number=7\r\nSet-Cookie: string=seven'
147128
148-
149- SerialCookie
150-
151- The SerialCookie expects that all values should be serialized using
152- pickle. As a result of serializing, SerialCookie can save almost any
153- Python object to a value, and recover the exact same object when the
154- cookie has been returned. (SerialCookie can yield some
155- strange-looking cookie values, however.)
156-
157- >>> C = cookies.SerialCookie()
158- >>> C["number"] = 7
159- >>> C["string"] = "seven"
160- >>> C["number"].value
161- 7
162- >>> C["string"].value
163- 'seven'
164- >>> C.output()
165- 'Set-Cookie: number="L7\\012."\r\nSet-Cookie: string="Vseven\\012p0\\012."'
166-
167- Be warned, however, if SerialCookie cannot de-serialize a value (because
168- it isn't a valid pickle'd object), IT WILL RAISE AN EXCEPTION.
169-
170-
171- SmartCookie
172-
173- The SmartCookie combines aspects of each of the other two flavors.
174- When setting a value in a dictionary-fashion, the SmartCookie will
175- serialize (ala pickle) the value *if and only if* it isn't a
176- Python string. String objects are *not* serialized. Similarly,
177- when the load() method parses out values, it attempts to de-serialize
178- the value. If it fails, then it fallsback to treating the value
179- as a string.
180-
181- >>> C = cookies.SmartCookie()
182- >>> C["number"] = 7
183- >>> C["string"] = "seven"
184- >>> C["number"].value
185- 7
186- >>> C["string"].value
187- 'seven'
188- >>> C.output()
189- 'Set-Cookie: number="L7\\012."\r\nSet-Cookie: string=seven'
190-
191-
192- Backwards Compatibility
193- -----------------------
194-
195- In order to keep compatibilty with earlier versions of Cookie.py,
196- it is still possible to use cookies.Cookie() to create a Cookie. In
197- fact, this simply returns a SmartCookie.
198-
199- >>> C = cookies.Cookie()
200- >>> print(C.__class__.__name__)
201- SmartCookie
202-
203-
204129Finis.
205130""" #"
206131# ^
215140
216141import re , warnings
217142
218- __all__ = ["CookieError" ,"BaseCookie" ,"SimpleCookie" ,"SerialCookie" ,
219- "SmartCookie" ,"Cookie" ]
143+ __all__ = ["CookieError" , "BaseCookie" , "SimpleCookie" ]
220144
221145_nulljoin = '' .join
222146_semispacejoin = '; ' .join
@@ -653,70 +577,6 @@ def value_encode(self, val):
653577 return strval , _quote ( strval )
654578# end SimpleCookie
655579
656- class SerialCookie (BaseCookie ):
657- """SerialCookie
658- SerialCookie supports arbitrary objects as cookie values. All
659- values are serialized (using pickle) before being sent to the
660- client. All incoming values are assumed to be valid Pickle
661- representations. IF AN INCOMING VALUE IS NOT IN A VALID PICKLE
662- FORMAT, THEN AN EXCEPTION WILL BE RAISED.
663-
664- Note: Large cookie values add overhead because they must be
665- retransmitted on every HTTP transaction.
666-
667- Note: HTTP has a 2k limit on the size of a cookie. This class
668- does not check for this limit, so be careful!!!
669- """
670- def __init__ (self , input = None ):
671- warnings .warn ("SerialCookie class is insecure; do not use it" ,
672- DeprecationWarning )
673- BaseCookie .__init__ (self , input )
674- # end __init__
675- def value_decode (self , val ):
676- # This could raise an exception!
677- return loads ( _unquote (val ).encode ('latin-1' ) ), val
678- def value_encode (self , val ):
679- return val , _quote ( dumps (val , 0 ).decode ('latin-1' ) )
680- # end SerialCookie
681-
682- class SmartCookie (BaseCookie ):
683- """SmartCookie
684- SmartCookie supports arbitrary objects as cookie values. If the
685- object is a string, then it is quoted. If the object is not a
686- string, however, then SmartCookie will use pickle to serialize
687- the object into a string representation.
688-
689- Note: Large cookie values add overhead because they must be
690- retransmitted on every HTTP transaction.
691-
692- Note: HTTP has a 2k limit on the size of a cookie. This class
693- does not check for this limit, so be careful!!!
694- """
695- def __init__ (self , input = None ):
696- warnings .warn ("Cookie/SmartCookie class is insecure; do not use it" ,
697- DeprecationWarning )
698- BaseCookie .__init__ (self , input )
699- # end __init__
700- def value_decode (self , val ):
701- strval = _unquote (val )
702- try :
703- return loads (strval .encode ('latin-1' )), val
704- except :
705- return strval , val
706- def value_encode (self , val ):
707- if isinstance (val , str ):
708- return val , _quote (val )
709- else :
710- return val , _quote ( dumps (val , 0 ).decode ('latin-1' ) )
711- # end SmartCookie
712-
713-
714- ###########################################################
715- # Backwards Compatibility: Don't break any existing code!
716-
717- # We provide Cookie() as an alias for SmartCookie()
718- Cookie = SmartCookie
719-
720580#
721581###########################################################
722582
@@ -726,8 +586,3 @@ def _test():
726586
727587if __name__ == "__main__" :
728588 _test ()
729-
730-
731- #Local Variables:
732- #tab-width: 4
733- #end:
0 commit comments