1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
from Cookie import SimpleCookie
import timeinterval
class Cookie(object):
def __init__(self, name, value, path, expires=None, secure=False):
self.name = name
self.value = value
self.path = path
self.secure = secure
if expires == 'ONCLOSE' or not expires:
expires = None
elif expires == 'NOW' or expires == 'NEVER':
expires = time.gmtime(time.time())
if expires == 'NEVER':
expires = (expires[0] + 10,) + expires[1:]
expires = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT", expires)
else:
if isinstance(expires, (str, unicode)) and expires.startswith('+'):
interval = timeinterval.time_decode(expires[1:])
expires = time.time() + interval
if isinstance(expires, (int, long, float)):
expires = time.gmtime(expires)
if isinstance(expires, (tuple, time.struct_time)):
expires = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT", expires)
self.expires = expires
def __repr__(self):
return '<%s %s=%r>' % (
self.__class__.__name__, self.name, self.value)
def header(self):
c = SimpleCookie()
c[self.name] = value
c[self.name]['path'] = self.path
if self.expires is not None:
c[self.name]['expires'] = expires
if self.secure:
c[self.name]['secure'] = True
return str(c).split(':')[1].strip()
|