mfc @sdZddlZyddlmZmZWn'ek rUddlmZmZnXddlZddlZdddddd gZ d j Z d j Z d j Z defd YZejejdZidd6dd6dd6dd6dd6dd6dd6dd6dd 6d!d"6d#d$6d%d&6d'd(6d)d*6d+d,6d-d.6d/d06d1d26d3d46d5d66d7d86d9d:6d;d<6d=d>6d?d@6dAdB6dCdD6dEdF6dGdH6dIdJ6dKdL6dMdN6dOdP6dQdR6dSdT6dUdV6dWdX6dYdZ6d[d\6d]d^6d_d`6dadb6dcdd6dedf6dgdh6didj6dkdl6dmdn6dodp6dqdr6dsdt6dudv6dwdx6dydz6d{d|6d}d~6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6d d 6d d 6d d6dd6dd6dd6dd6dd6dd6dd6dd6dd 6d!d"6d#d$6d%d&6d'd(6d)d*6d+d,6d-d.6d/d06d1d26d3d46d5d66d7d86d9d:6d;d<6d=d>6d?d@6dAdB6dCdD6dEdF6dGdH6dIdJ6dKdL6dMdN6dOdP6dQdR6dSdT6dUdV6dWdX6Zd j dYedZDZeeejd[Zejd\jZd]Zd^Zd_d`dadbdcdddegZddfdgdhdidjdkdldmdndodpdqg ZdreedsZ dte!fduYZ"dvZ#ejdwe#dxe#dyZ$de!fdzYZ%de%fd{YZ&de%fd|YZ'de%fd}YZ(e(Z)d~Z*e+dkre*ndS(s) 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 slightly different encoding semantics, 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" >>> C.output() 'Set-Cookie: fig=newton\r\nSet-Cookie: sugar=wafer' 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 .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") >>> C.output() 'Set-Cookie: chips=ahoy\r\nSet-Cookie: vienna=finger' 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' >>> C.output() 'Set-Cookie: number=7\r\nSet-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' >>> C.output() 'Set-Cookie: number="I7\\012."\r\nSet-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' >>> C.output() 'Set-Cookie: number="I7\\012."\r\nSet-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. iN(tdumpstloadst CookieErrort BaseCookiet SimpleCookiet SerialCookiet SmartCookietCookiets; t cBseZRS((t__name__t __module__(((s/usr/lib64/python2.7/Cookie.pyRss!#$%&'*+-.^_`|~s\000ts\001ss\002ss\003ss\004ss\005ss\006ss\007ss\010ss\011s s\012s s\013s s\014s s\015s s\016ss\017ss\020ss\021ss\022ss\023ss\024ss\025ss\026ss\027ss\030ss\031ss\032ss\033ss\034ss\035ss\036ss\037ss\054t,s\073t;s\"t"s\\s\s\177ss\200ss\201ss\202ss\203ss\204ss\205ss\206ss\207ss\210ss\211ss\212ss\213ss\214ss\215ss\216ss\217ss\220ss\221ss\222ss\223ss\224ss\225ss\226ss\227ss\230ss\231ss\232ss\233ss\234ss\235ss\236ss\237ss\240ss\241ss\242ss\243ss\244ss\245ss\246ss\247ss\250ss\251ss\252ss\253ss\254ss\255ss\256ss\257ss\260ss\261ss\262ss\263ss\264ss\265ss\266ss\267ss\270ss\271ss\272ss\273ss\274ss\275ss\276ss\277ss\300ss\301ss\302ss\303ss\304ss\305ss\306ss\307ss\310ss\311ss\312ss\313ss\314ss\315ss\316ss\317ss\320ss\321ss\322ss\323ss\324ss\325ss\326ss\327ss\330ss\331ss\332ss\333ss\334ss\335ss\336ss\337ss\340ss\341ss\342ss\343ss\344ss\345ss\346ss\347ss\350ss\351ss\352ss\353ss\354ss\355ss\356ss\357ss\360ss\361ss\362ss\363ss\364ss\365ss\366ss\367ss\370ss\371ss\372ss\373ss\374ss\375ss\376ss\377sccs|]}t|VqdS(N(tchr(t.0tx((s/usr/lib64/python2.7/Cookie.pys 9sicCsAd||||kr|Sdtttj||dSdS(NRR(t _nulljointmapt _Translatortget(tstrt LegalCharstidmapt translate((s/usr/lib64/python2.7/Cookie.pyt_quote;ss\\(?:([0-3][0-7][0-7])|(.))cCs<|jdr+tt|jddS|jdSdS(Niii(tgroupRtint(tm((s/usr/lib64/python2.7/Cookie.pyt_unquote_replaceKscCsTt|dkr|S|ddks6|ddkr:|S|dd!}tt|S(NiiRii(tlent _unquote_subR(R((s/usr/lib64/python2.7/Cookie.pyt_unquoteQs   tMontTuetWedtThutFritSattSuntJantFebtMartAprtMaytJuntJultAugtSeptOcttNovtDecic Csoddlm}m}|}|||\ }}}} } } } } }d|| ||||| | | fS(Ni(tgmtimettimes#%s, %02d %3s %4d %02d:%02d:%02d GMT(R7R6(tfuturet weekdaynamet monthnameR6R7tnowtyeartmonthtdaythhtmmtsstwdtytz((s/usr/lib64/python2.7/Cookie.pyt_getdatets  +tMorselcBseZidd6dd6dd6dd6dd6d d 6d d 6d d 6Zd ZdZdZeeej dZ dddZ e Z dZddZddZRS(texpirestPathtpathtCommenttcommenttDomaintdomainsMax-Agesmax-agetsecurethttponlytVersiontversioncCsBd|_|_|_x$|jD]}tj||dq!WdS(NR(tNonetkeytvaluet coded_valuet _reservedtdictt __setitem__(tselftK((s/usr/lib64/python2.7/Cookie.pyt__init__scCsE|j}||jkr.td|ntj|||dS(NsInvalid Attribute %s(tlowerRVRRWRX(RYRZtV((s/usr/lib64/python2.7/Cookie.pyRXs cCs|j|jkS(N(R\RV(RYRZ((s/usr/lib64/python2.7/Cookie.pyt isReservedKeyscCsr|j|jkr(td|nd||||krStd|n||_||_||_dS(Ns!Attempt to set a reserved key: %sRsIllegal key value: %s(R\RVRRSRTRU(RYRStvalt coded_valRRR((s/usr/lib64/python2.7/Cookie.pytsets  s Set-Cookie:cCsd||j|fS(Ns%s %s(t OutputString(RYtattrstheader((s/usr/lib64/python2.7/Cookie.pytoutputscCs#d|jj|jt|jfS(Ns <%s: %s=%s>(t __class__R RStreprRT(RY((s/usr/lib64/python2.7/Cookie.pyt__repr__s cCs d|j|jddfS(Ns Rs\"(Rbtreplace(RYRc((s/usr/lib64/python2.7/Cookie.pyt js_outputscCsg}|j}|d|j|jf|dkrA|j}n|j}|jx)|D]!\}}|dkr|q^n||krq^n|dkrt|tdkr|d|j|t|fq^|dkrt|tdkr|d|j||fq^|dkr>|t |j|q^|dkrd|t |j|q^|d|j||fq^Wt |S( Ns%s=%sRRGismax-ages%s=%dRNRO( tappendRSRURRRVtitemstsortttypeRERt_semispacejoin(RYRctresulttRARlRZR]((s/usr/lib64/python2.7/Cookie.pyRbs,       $$$  N(R R RVR[RXR^t _LegalCharst_idmaptstringRRaRRRet__str__RhRjRb(((s/usr/lib64/python2.7/Cookie.pyRFs$      s.[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]s (?x)(?PsK+?)\s*=\s*(?P"(?:[^\\"]|\\.)*"|\w{3},\s[\s\w\d-]{9,11}\s[\d:]{8}\sGMT|s*)\s*;?cBszeZdZdZd dZdZdZd dddZeZ dZ d d Z d Z e d ZRS( cCs ||fS(s 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. ((RYR_((s/usr/lib64/python2.7/Cookie.pyt value_decodescCst|}||fS(sreal_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. (R(RYR_tstrval((s/usr/lib64/python2.7/Cookie.pyt value_encode's cCs|r|j|ndS(N(tload(RYtinput((s/usr/lib64/python2.7/Cookie.pyR[1scCs?|j|t}|j|||tj|||dS(s+Private method for setting a cookie's valueN(RRFRaRWRX(RYRSt real_valueRUtM((s/usr/lib64/python2.7/Cookie.pyt__set5scCs,|j|\}}|j|||dS(sDictionary style assignment.N(Rxt_BaseCookie__set(RYRSRTtrvaltcval((s/usr/lib64/python2.7/Cookie.pyRX<ss Set-Cookie:s cCsYg}|j}|jx-|D]%\}}|j|j||q#W|j|S(s"Return a string suitable for HTTP.(RlRmRkRetjoin(RYRcRdtsepRpRlRZR]((s/usr/lib64/python2.7/Cookie.pyReBs   cCsmg}|j}|jx4|D],\}}|jd|t|jfq#Wd|jjt|fS(Ns%s=%ss<%s: %s>(RlRmRkRgRTRfR t _spacejoin(RYtLRlRZR]((s/usr/lib64/python2.7/Cookie.pyRhNs   $cCsSg}|j}|jx*|D]"\}}|j|j|q#Wt|S(s(Return a string suitable for JavaScript.(RlRmRkRjR(RYRcRpRlRZR]((s/usr/lib64/python2.7/Cookie.pyRjVs   cCsSt|tdkr(|j|n'x$|jD]\}}|||s         |  t$