<s> import functools <EOL> import socket <EOL> from base64 import b64encode , b64decode <EOL> from tornado . ioloop import IOLoop <EOL> from tornado . iostream import IOStream <EOL> class Memnado ( object ) : <EOL> def __init__ ( self , host , port ) : <EOL> self . host = host <EOL> self . port = port <EOL> s = socket . socket ( socket . AF_INET , socket . SOCK_STREAM , <NUM_LIT:0> ) <EOL> s . connect ( ( self . host , self . port ) ) <EOL> self . stream = IOStream ( s ) <EOL> def set ( self , key , value , callback , expiry = <NUM_LIT:0> ) : <EOL> key = b64encode ( key ) <EOL> value = b64encode ( value ) <EOL> content_length = len ( value ) <EOL> self . stream . write ( "<STR_LIT>" % ( key , expiry , <EOL> content_length , value ) ) <EOL> self . stream . read_until ( "<STR_LIT:\r\n>" , callback ) <EOL> def get ( self , key , callback ) : <EOL> key = b64encode ( key ) <EOL> def process_get ( stream , cb , data ) : <EOL> if data [ <NUM_LIT:0> : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> cb ( None ) <EOL> else : <EOL> status , k , flags , content_length = data . strip ( ) . split ( '<STR_LIT:U+0020>' ) <EOL> def wrapped_cb ( f ) : <EOL> return lambda data : f ( b64decode ( data ) ) <EOL> stream . read_bytes ( int ( content_length ) , wrapped_cb ( cb ) ) <EOL> stream . read_until ( "<STR_LIT>" , lambda d : d ) <EOL> self . stream . write ( "<STR_LIT>" % key ) <EOL> self . stream . read_until ( "<STR_LIT:\r\n>" , functools . partial ( process_get , self . stream , callback ) ) </s>
