-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTornadoCookieJar.py
170 lines (123 loc) · 4.64 KB
/
TornadoCookieJar.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import requests.cookies
from urlparse import urlparse
import traceback
import urllib
import time
import Cookie
class TornadoCookieJar(object):
_reserved = { "expires" : "expires",
"path" : "Path",
"comment" : "Comment",
"domain" : "Domain",
"max-age" : "Max-Age",
"secure" : "secure",
"httponly" : "httponly",
"version" : "Version",
}
_weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
_monthname = [None,
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
_semispacejoin = '; '.join
@staticmethod
def _getdate(timestamp=0, weekdayname=_weekdayname, monthname=_monthname):
from time import gmtime, time
now = timestamp
year, month, day, hh, mm, ss, wd, y, z = gmtime(now)
return "%s, %02d-%3s-%4d %02d:%02d:%02d GMT" % \
(weekdayname[wd], day, monthname[month], year, hh, mm, ss)
@staticmethod
def cookie_to_set_cookie_header(cookie):
# Build up our result
#
result = []
RA = result.append
items = []
name = ''
value = ''
properties = [method for method in dir(cookie)]
for x in properties:
if x.lower() == 'name':
name = getattr(cookie, x)
continue
elif x.lower() == 'value':
value = getattr(cookie, x)
continue
else:
if x not in TornadoCookieJar._reserved:
continue
items.append((x, getattr(cookie, x)))
if len(name) < 1 or len(value) < 1:
return None
RA('%s=%s' % (name, urllib.quote_plus(value)))
items.sort()
for K,V in items:
if V == "": continue
# fix expires
if K == 'expires' and V is None or len(str(V)) < 1:
V = int(time.time() + 31536000) # 1 year
if K == "expires" and type(V) == type(1):
RA("%s=%s" % (TornadoCookieJar._reserved[K], TornadoCookieJar._getdate(V)))
elif K == "max-age" and type(V) == type(1):
RA("%s=%d" % (TornadoCookieJar._reserved[K], V))
elif K == "secure":
RA(str(TornadoCookieJar._reserved[K]))
elif K == "httponly":
RA(str(TornadoCookieJar._reserved[K]))
else:
RA("%s=%s" % (TornadoCookieJar._reserved[K], V))
return TornadoCookieJar._semispacejoin(result)
def __init__(self):
self.jar = requests.cookies.RequestsCookieJar()
def reset(self):
self.jar = requests.cookies.RequestsCookieJar()
def handleResponse(self, response):
try:
if response is None:
return
if not hasattr(response, 'headers'):
return
if response.headers is None:
return
lst = response.headers.get_list("Set-Cookie")
for sc in lst:
c = Cookie.SimpleCookie(sc)
for morsel in c.values():
if morsel['max-age']:
morsel['max-age'] = int(morsel['max-age'])
cookie = requests.cookies.morsel_to_cookie(morsel)
self.jar.set_cookie(cookie)
except Exception as e:
print 'COOKIE JAR HANDLE RESPONSE ERROR'
print e
traceback.print_exc()
#print self.jar
def add_cookie_by_string(self, str):
if str is not None and len(str) >= 1:
c = Cookie.SimpleCookie(str)
for morsel in c.values():
if morsel['max-age']:
morsel['max-age'] = int(morsel['max-age'])
cookie = requests.cookies.morsel_to_cookie(morsel)
self.jar.set_cookie(cookie)
def getCookieHeaderDictForUrl(self, url):
details = urlparse(url)
domain = details.netloc
path = details.path
dict = self.jar.get_dict()
if not dict:
return {}
return dict
def getCookieHeaderStringForUrl(self, url):
details = urlparse(url)
domain = details.netloc
path = details.path
#print 'urlparse: [host=%s, path=%s]' % (domain, path)
dict = self.jar.get_dict()
#print 'cookiedict: [%s]' % dict
if not dict:
return ''
attrs = []
for (key, value) in dict.items():
attrs.append("%s=%s" % (key, value))
return "; ".join(attrs)