-
Notifications
You must be signed in to change notification settings - Fork 2
/
memnado.py
44 lines (32 loc) · 1.39 KB
/
memnado.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
import functools
import socket
from base64 import b64encode, b64decode
from tornado.ioloop import IOLoop
from tornado.iostream import IOStream
class Memnado(object):
def __init__(self, host, port):
self.host = host
self.port = port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
s.connect((self.host, self.port))
self.stream = IOStream(s)
def set(self, key, value, callback, expiry=0):
key = b64encode(key)
value = b64encode(value)
content_length = len(value)
self.stream.write("set %s 1 %s %s\r\n%s\r\n" % (key, expiry,
content_length, value))
self.stream.read_until("\r\n", callback)
def get(self, key, callback):
key = b64encode(key)
def process_get(stream, cb, data):
if data[0:3] == 'END': # key is empty
cb(None)
else:
status, k, flags, content_length = data.strip().split(' ')
def wrapped_cb(f):
return lambda data: f(b64decode(data))
stream.read_bytes(int(content_length), wrapped_cb(cb))
stream.read_until("\r\nEND\r\n", lambda d: d)
self.stream.write("get %s\r\n" % key)
self.stream.read_until("\r\n", functools.partial(process_get, self.stream, callback))