Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: 34bc77143a
Fetching contributors…

Cannot retrieve contributors at this time

42 lines (30 sloc) 1.359 kB
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): = host
self.port = port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
s.connect((, self.port)) = IOStream(s)
def set(self, key, value, callback, expiry=0):
value = b64encode(value)
content_length = len(value)"set %s 1 %s %s\r\n%s\r\n" % (key, expiry,
content_length, value))"\r\n", callback)
def get(self, key, callback):
def process_get(stream, cb, data):
if data[0:3] == 'END': # key is empty
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)"get %s\r\n" % key)"\r\n", functools.partial(process_get,, callback))
Jump to Line
Something went wrong with that request. Please try again.