/
kt_http.py
215 lines (169 loc) · 5.72 KB
/
kt_http.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
#
# Copyright 2011, Toru Maesaka
#
# Redistribution and use of this source code is licensed under
# the BSD license. See COPYING file for license description.
import httplib
import urllib
import struct
import time
import kt_error
try:
import cPickle as pickle
except ImportError:
import pickle
# Stick with URL encoding for now. Eventually run a benchmark
# to evaluate what the most approariate encoding algorithm is.
KT_HTTP_HEADER = {
'Content-Type' : 'text/tab-separated-values; colenc=U',
}
class ProtocolHandler:
def __init__(self, pickle_protocol=2):
self.error_obj = kt_error.KyotoTycoonError()
self.pickle_protocol = pickle_protocol
self.pack = self._pickle_packer
self.unpack = self._pickle_unpacker
def error(self):
return self.error_obj
def open(self, host, port, timeout):
try:
self.conn = httplib.HTTPConnection(host, port, timeout)
except Exception, e:
raise e
return True
def close(self):
try:
self.conn.close()
except Exception, e:
raise e
return True
def echo(self):
self.conn.request('POST', '/rpc/echo')
res = self.conn.getresponse()
body = res.read()
return True if res.status == 200 else False
def get(self, key):
if key is None:
return False
key = urllib.quote(key.encode('UTF-8'))
self.conn.request('GET', key)
rv = self.conn.getresponse()
body = rv.read()
if rv.status != 200:
return None
return self.unpack(body)
def get_int(self, key):
if key is None: return False
key = urllib.quote(key.encode('UTF-8'))
self.conn.request('GET', key)
rv = self.conn.getresponse()
buf = rv.read()
if rv.status != 200:
return None
return struct.unpack('>q', buf)[0]
def set(self, key, value, expire):
if key is None:
return False
key = urllib.quote(key.encode('UTF-8'))
value = self.pack(value)
return self._rest_put(key, value, expire) == 201
def set_int(self, key, value, expire):
if key is None:
return False
if not isinstance(value, int):
return False
key = urllib.quote(key.encode('UTF-8'))
value = struct.pack('>q', value)
return self._rest_put(key, value, expire) == 201
def add(self, key, value, expire):
if key is None:
return False
headers = { 'X-Kt-Mode' : 'add' }
if expire != None:
expire = int(time.time()) + expire;
headers["X-Kt-Xt"] = str(expire)
key = urllib.quote(key.encode('UTF-8'))
value = self.pack(value)
self.conn.request('PUT', key, value, headers)
rv = self.conn.getresponse()
body = rv.read()
return rv.status == 201;
def remove(self, key):
if key is None: return False
key = urllib.quote(key.encode('UTF-8'))
self.conn.request('DELETE', key)
rv = self.conn.getresponse()
body = rv.read()
return rv.status == 204
def replace(self, key, value, expire):
if key is None: return False
headers = { 'X-Kt-Mode' : 'replace' }
if expire != None:
expire = int(time.time()) + expire;
headers["X-Kt-Xt"] = str(expire)
key = urllib.quote(key.encode('UTF-8'))
value = self.pack(value)
self.conn.request('PUT', key, value, headers)
rv = self.conn.getresponse()
body = rv.read()
return rv.status == 201
def increment(self, key, delta, expire):
if key is None: return False
key = urllib.quote(key.encode('UTF-8'))
delta = int(delta)
request_body = 'key\t%s\nnum\t%d\n' % (key, delta)
self.conn.request('POST', '/rpc/increment', body=request_body,
headers=KT_HTTP_HEADER)
res = self.conn.getresponse()
body = res.read()
if res.status != 200:
return None
return int(self._tsv_to_dict(body)['num'])
def report(self):
self.conn.request('GET', '/rpc/report')
res = self.conn.getresponse()
body = res.read()
if res.status != 200:
return None
return self._tsv_to_dict(body)
def status(self):
self.conn.request('GET', '/rpc/status')
res = self.conn.getresponse()
body = res.read()
if res.status != 200:
return None
return self._tsv_to_dict(body)
def clear(self):
self.conn.request('GET', '/rpc/clear')
res = self.conn.getresponse()
body = res.read()
return True if res.status == 200 else False
def count(self):
dict = self.status()
return int(dict['count'])
def size(self):
dict = self.status()
return int(dict['size'])
def _dict_to_tsv(self, dict):
return '\n'.join(k + '\t' + str(v) for (k, v) in dict.items())
def _tsv_to_dict(self, tsv_str):
rv = {}
for row in tsv_str.split('\n'):
kv = row.split('\t')
if len(kv) == 2:
rv[kv[0]] = kv[1]
return rv
def _rest_put(self, key, value, expire):
headers = {}
if expire != None:
expire = int(time.time()) + expire;
headers["X-Kt-Xt"] = str(expire)
self.conn.request('PUT', key, value, headers)
rv = self.conn.getresponse()
body = rv.read()
return rv.status
def _pickle_packer(self, data):
return pickle.dumps(data, self.pickle_protocol)
def _pickle_unpacker(self, data):
return pickle.loads(data)