<s> """<STR_LIT>""" <EOL> import sys <EOL> import socket <EOL> import time <EOL> import os <EOL> import re <EOL> import types <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> try : <EOL> from zlib import compress , decompress <EOL> _supports_compress = True <EOL> except ImportError : <EOL> _supports_compress = False <EOL> def decompress ( val ) : <EOL> raise _Error ( "<STR_LIT>" ) <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> from binascii import crc32 <EOL> serverHashFunction = crc32 <EOL> __author__ = "<STR_LIT>" <EOL> __version__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> SERVER_MAX_KEY_LENGTH = <NUM_LIT> <EOL> SERVER_MAX_VALUE_LENGTH = <NUM_LIT> ** <NUM_LIT:3> <EOL> MAX_ROWS = <NUM_LIT:100> <EOL> class _Error ( Exception ) : <EOL> pass <EOL> try : <EOL> from threading import local <EOL> except ImportError : <EOL> class local ( object ) : <EOL> pass <EOL> class Client ( local ) : <EOL> """<STR_LIT>""" <EOL> _FLAG_PICKLE = <NUM_LIT:1> << <NUM_LIT:0> <EOL> _FLAG_INTEGER = <NUM_LIT:1> << <NUM_LIT:1> <EOL> _FLAG_LONG = <NUM_LIT:1> << <NUM_LIT:2> <EOL> _FLAG_COMPRESSED = <NUM_LIT:1> << <NUM_LIT:3> <EOL> _SERVER_RETRIES = <NUM_LIT:10> <EOL> class MemcachedKeyError ( Exception ) : <EOL> pass <EOL> class MemcachedKeyLengthError ( MemcachedKeyError ) : <EOL> pass <EOL> class MemcachedKeyCharacterError ( MemcachedKeyError ) : <EOL> pass <EOL> class MemcachedStringEncodingError ( Exception ) : <EOL> pass <EOL> def __init__ ( self , servers , debug = <NUM_LIT:0> , pickleProtocol = <NUM_LIT:0> , <EOL> pickler = pickle . Pickler , unpickler = pickle . Unpickler , <EOL> pload = None , pid = None ) : <EOL> """<STR_LIT>""" <EOL> local . __init__ ( self ) <EOL> self . set_servers ( servers ) <EOL> self . debug = debug <EOL> self . stats = { } <EOL> self . pickleProtocol = pickleProtocol <EOL> self . pickler = pickler <EOL> self . unpickler = unpickler <EOL> self . persistent_load = pload <EOL> self . persistent_id = pid <EOL> def set_servers ( self , servers ) : <EOL> """<STR_LIT>""" <EOL> self . servers = [ _Host ( s , self . debuglog ) for s in servers ] <EOL> self . _init_buckets ( ) <EOL> def get_stats ( self , subcmd = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> data = [ ] <EOL> for s in self . servers : <EOL> if not s . connect ( ) : continue <EOL> name = '<STR_LIT>' % ( s . ip , s . port , s . weight ) <EOL> if subcmd == '<STR_LIT>' : <EOL> s . send_cmd ( '<STR_LIT>' ) <EOL> else : <EOL> s . send_cmd ( '<STR_LIT>' % subcmd ) <EOL> serverData = { } <EOL> data . append ( ( name , serverData ) ) <EOL> readline = s . readline <EOL> while <NUM_LIT:1> : <EOL> line = readline ( ) <EOL> if not line or line . strip ( ) == '<STR_LIT>' : break <EOL> stats = line . split ( '<STR_LIT:U+0020>' , <NUM_LIT:2> ) <EOL> serverData [ stats [ <NUM_LIT:1> ] ] = stats [ <NUM_LIT:2> ] <EOL> return ( data ) <EOL> def rget ( self , start_key , end_key , is_left_open , is_right_open , limit ) : <EOL> check_key ( start_key ) <EOL> check_key ( end_key ) <EOL> if is_left_open != <NUM_LIT:0> : <EOL> is_left_open = <NUM_LIT:1> <EOL> if is_right_open != <NUM_LIT:0> : <EOL> is_right_open = <NUM_LIT:1> <EOL> if limit > MAX_ROWS or limit <= <NUM_LIT:0> : <EOL> limit = MAX_ROWS <EOL> self . _statlog ( '<STR_LIT>' ) <EOL> retvals = [ ] <EOL> for server in self . servers : <EOL> if not server . connect ( ) : continue <EOL> try : <EOL> server . send_cmd ( '<STR_LIT>' % ( start_key , end_key , is_left_open , is_right_open , limit ) ) <EOL> line = server . readline ( ) <EOL> while line and line != '<STR_LIT>' : <EOL> rkey , flags , rlen = self . _expectvalue ( server , line ) <EOL> if rkey is not None : <EOL> val = self . _recv_value ( server , flags , rlen ) <EOL> retvals . append ( ( rkey , val ) ) <EOL> line = server . readline ( ) <EOL> except ( _Error , socket . error ) , msg : <EOL> if type ( msg ) is types . TupleType : msg = msg [ <NUM_LIT:1> ] <EOL> server . mark_dead ( msg ) <EOL> return ( retvals ) <EOL> def db_archive ( self ) : <EOL> '<STR_LIT>' <EOL> for s in self . servers : <EOL> if not s . connect ( ) : continue <EOL> s . send_cmd ( '<STR_LIT>' ) <EOL> return ( s . expect ( "<STR_LIT:OK>" ) == "<STR_LIT:OK>" ) <EOL> def db_checkpoint ( self ) : <EOL> '<STR_LIT>' <EOL> for s in self . servers : <EOL> if not s . connect ( ) : continue <EOL> s . send_cmd ( '<STR_LIT>' ) <EOL> return ( s . expect ( "<STR_LIT:OK>" ) == "<STR_LIT:OK>" ) <EOL> def rep_set_priority ( self , priority ) : <EOL> '<STR_LIT>' <EOL> for s in self . servers : <EOL> if not s . connect ( ) : continue <EOL> s . send_cmd ( '<STR_LIT>' % priority ) <EOL> return ( s . expect ( "<STR_LIT:OK>" ) == "<STR_LIT:OK>" ) <EOL> def rep_set_ack_policy ( self , ack_policy ) : <EOL> '<STR_LIT>' <EOL> for s in self . servers : <EOL> if not s . connect ( ) : continue <EOL> s . send_cmd ( '<STR_LIT>' % ack_policy ) <EOL> return ( s . expect ( "<STR_LIT:OK>" ) == "<STR_LIT:OK>" ) <EOL> def flush_all ( self ) : <EOL> '<STR_LIT>' <EOL> for s in self . servers : <EOL> if not s . connect ( ) : continue <EOL> s . send_cmd ( '<STR_LIT>' ) <EOL> s . expect ( "<STR_LIT:OK>" ) <EOL> def debuglog ( self , str ) : <EOL> if self . debug : <EOL> sys . stderr . write ( "<STR_LIT>" % str ) <EOL> def _statlog ( self , func ) : <EOL> if not self . stats . has_key ( func ) : <EOL> self . stats [ func ] = <NUM_LIT:1> <EOL> else : <EOL> self . stats [ func ] += <NUM_LIT:1> <EOL> def forget_dead_hosts ( self ) : <EOL> """<STR_LIT>""" <EOL> for s in self . servers : <EOL> s . dead_until = <NUM_LIT:0> <EOL> def _init_buckets ( self ) : <EOL> self . buckets = [ ] <EOL> for server in self . servers : <EOL> for i in range ( server . weight ) : <EOL> self . buckets . append ( server ) <EOL> def _get_server ( self , key ) : <EOL> if type ( key ) == types . TupleType : <EOL> serverhash , key = key <EOL> else : <EOL> serverhash = serverHashFunction ( key ) <EOL> for i in range ( Client . _SERVER_RETRIES ) : <EOL> server = self . buckets [ serverhash % len ( self . buckets ) ] <EOL> if server . connect ( ) : <EOL> return server , key <EOL> serverhash = serverHashFunction ( str ( serverhash ) + str ( i ) ) <EOL> return None , None <EOL> def disconnect_all ( self ) : <EOL> for s in self . servers : <EOL> s . close_socket ( ) <EOL> def delete_multi ( self , keys , time = <NUM_LIT:0> , key_prefix = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> self . _statlog ( '<STR_LIT>' ) <EOL> server_keys , prefixed_to_orig_key = self . _map_and_prefix_keys ( keys , key_prefix ) <EOL> dead_servers = [ ] <EOL> rc = <NUM_LIT:1> <EOL> for server in server_keys . iterkeys ( ) : <EOL> bigcmd = [ ] <EOL> write = bigcmd . append <EOL> if time != None : <EOL> for key in server_keys [ server ] : <EOL> write ( "<STR_LIT>" % ( key , time ) ) <EOL> else : <EOL> for key in server_keys [ server ] : <EOL> write ( "<STR_LIT>" % key ) <EOL> try : <EOL> server . send_cmds ( '<STR_LIT>' . join ( bigcmd ) ) <EOL> except socket . error , msg : <EOL> rc = <NUM_LIT:0> <EOL> if type ( msg ) is types . TupleType : msg = msg [ <NUM_LIT:1> ] <EOL> server . mark_dead ( msg ) <EOL> dead_servers . append ( server ) <EOL> for server in dead_servers : <EOL> del server_keys [ server ] <EOL> notstored = [ ] <EOL> for server , keys in server_keys . iteritems ( ) : <EOL> try : <EOL> for key in keys : <EOL> server . expect ( "<STR_LIT>" ) <EOL> except socket . error , msg : <EOL> if type ( msg ) is types . TupleType : msg = msg [ <NUM_LIT:1> ] <EOL> server . mark_dead ( msg ) <EOL> rc = <NUM_LIT:0> <EOL> return rc <EOL> def delete ( self , key , time = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> check_key ( key ) <EOL> server , key = self . _get_server ( key ) <EOL> if not server : <EOL> return <NUM_LIT:0> <EOL> self . _statlog ( '<STR_LIT>' ) <EOL> if time != None : <EOL> cmd = "<STR_LIT>" % ( key , time ) <EOL> else : <EOL> cmd = "<STR_LIT>" % key <EOL> try : <EOL> server . send_cmd ( cmd ) <EOL> server . expect ( "<STR_LIT>" ) <EOL> except socket . error , msg : <EOL> if type ( msg ) is types . TupleType : msg = msg [ <NUM_LIT:1> ] <EOL> server . mark_dead ( msg ) <EOL> return <NUM_LIT:0> <EOL> return <NUM_LIT:1> <EOL> def incr ( self , key , delta = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . _incrdecr ( "<STR_LIT>" , key , delta ) <EOL> def decr ( self , key , delta = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . _incrdecr ( "<STR_LIT>" , key , delta ) <EOL> def _incrdecr ( self , cmd , key , delta ) : <EOL> check_key ( key ) <EOL> server , key = self . _get_server ( key ) <EOL> if not server : <EOL> return <NUM_LIT:0> <EOL> self . _statlog ( cmd ) <EOL> cmd = "<STR_LIT>" % ( cmd , key , delta ) <EOL> try : <EOL> server . send_cmd ( cmd ) <EOL> line = server . readline ( ) <EOL> return int ( line ) <EOL> except socket . error , msg : <EOL> if type ( msg ) is types . TupleType : msg = msg [ <NUM_LIT:1> ] <EOL> server . mark_dead ( msg ) <EOL> return None <EOL> def add ( self , key , val , time = <NUM_LIT:0> , min_compress_len = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> return self . _set ( "<STR_LIT>" , key , val , time , min_compress_len ) <EOL> def append ( self , key , val , time = <NUM_LIT:0> , min_compress_len = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> return self . _set ( "<STR_LIT>" , key , val , time , min_compress_len ) <EOL> def prepend ( self , key , val , time = <NUM_LIT:0> , min_compress_len = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> return self . _set ( "<STR_LIT>" , key , val , time , min_compress_len ) <EOL> def replace ( self , key , val , time = <NUM_LIT:0> , min_compress_len = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> return self . _set ( "<STR_LIT:replace>" , key , val , time , min_compress_len ) <EOL> def set ( self , key , val , time = <NUM_LIT:0> , min_compress_len = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> return self . _set ( "<STR_LIT>" , key , val , time , min_compress_len ) <EOL> def _map_and_prefix_keys ( self , key_iterable , key_prefix ) : <EOL> """<STR_LIT>""" <EOL> key_extra_len = len ( key_prefix ) <EOL> if key_prefix : <EOL> check_key ( key_prefix ) <EOL> server_keys = { } <EOL> prefixed_to_orig_key = { } <EOL> for orig_key in key_iterable : <EOL> if type ( orig_key ) is types . TupleType : <EOL> str_orig_key = str ( orig_key [ <NUM_LIT:1> ] ) <EOL> server , key = self . _get_server ( ( orig_key [ <NUM_LIT:0> ] , key_prefix + str_orig_key ) ) <EOL> else : <EOL> str_orig_key = str ( orig_key ) <EOL> server , key = self . _get_server ( key_prefix + str_orig_key ) <EOL> check_key ( str_orig_key , key_extra_len = key_extra_len ) <EOL> if not server : <EOL> continue <EOL> if not server_keys . has_key ( server ) : <EOL> server_keys [ server ] = [ ] <EOL> server_keys [ server ] . append ( key ) <EOL> prefixed_to_orig_key [ key ] = orig_key <EOL> return ( server_keys , prefixed_to_orig_key ) <EOL> def set_multi ( self , mapping , time = <NUM_LIT:0> , key_prefix = '<STR_LIT>' , min_compress_len = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> self . _statlog ( '<STR_LIT>' ) <EOL> server_keys , prefixed_to_orig_key = self . _map_and_prefix_keys ( mapping . iterkeys ( ) , key_prefix ) <EOL> dead_servers = [ ] <EOL> for server in server_keys . iterkeys ( ) : <EOL> bigcmd = [ ] <EOL> write = bigcmd . append <EOL> try : <EOL> for key in server_keys [ server ] : <EOL> store_info = self . _val_to_store_info ( mapping [ prefixed_to_orig_key [ key ] ] , min_compress_len ) <EOL> write ( "<STR_LIT>" % ( key , store_info [ <NUM_LIT:0> ] , time , store_info [ <NUM_LIT:1> ] , store_info [ <NUM_LIT:2> ] ) ) <EOL> server . send_cmds ( '<STR_LIT>' . join ( bigcmd ) ) <EOL> except socket . error , msg : <EOL> if type ( msg ) is types . TupleType : msg = msg [ <NUM_LIT:1> ] <EOL> server . mark_dead ( msg ) <EOL> dead_servers . append ( server ) <EOL> for server in dead_servers : <EOL> del server_keys [ server ] <EOL> if not server_keys : return ( mapping . keys ( ) ) <EOL> notstored = [ ] <EOL> for server , keys in server_keys . iteritems ( ) : <EOL> try : <EOL> for key in keys : <EOL> line = server . readline ( ) <EOL> if line == '<STR_LIT>' : <EOL> continue <EOL> else : <EOL> notstored . append ( prefixed_to_orig_key [ key ] ) <EOL> except ( _Error , socket . error ) , msg : <EOL> if type ( msg ) is types . TupleType : msg = msg [ <NUM_LIT:1> ] <EOL> server . mark_dead ( msg ) <EOL> return notstored <EOL> def _val_to_store_info ( self , val , min_compress_len ) : <EOL> """<STR_LIT>""" <EOL> flags = <NUM_LIT:0> <EOL> if isinstance ( val , str ) : <EOL> pass <EOL> elif isinstance ( val , int ) : <EOL> flags |= Client . _FLAG_INTEGER <EOL> val = "<STR_LIT>" % val <EOL> min_compress_len = <NUM_LIT:0> <EOL> elif isinstance ( val , long ) : <EOL> flags |= Client . _FLAG_LONG <EOL> val = "<STR_LIT>" % val <EOL> min_compress_len = <NUM_LIT:0> <EOL> else : <EOL> flags |= Client . _FLAG_PICKLE <EOL> file = StringIO ( ) <EOL> pickler = self . pickler ( file , protocol = self . pickleProtocol ) <EOL> if self . persistent_id : <EOL> pickler . persistent_id = self . persistent_id <EOL> pickler . dump ( val ) <EOL> val = file . getvalue ( ) <EOL> if len ( val ) >= SERVER_MAX_VALUE_LENGTH : return ( <NUM_LIT:0> ) <EOL> lv = len ( val ) <EOL> if min_compress_len and _supports_compress and lv > min_compress_len : <EOL> comp_val = compress ( val ) <EOL> if len ( comp_val ) < lv : <EOL> flags |= Client . _FLAG_COMPRESSED <EOL> val = comp_val <EOL> return ( flags , len ( val ) , val ) <EOL> def _set ( self , cmd , key , val , time , min_compress_len = <NUM_LIT:0> ) : <EOL> check_key ( key ) <EOL> server , key = self . _get_server ( key ) <EOL> if not server : <EOL> return <NUM_LIT:0> <EOL> self . _statlog ( cmd ) <EOL> store_info = self . _val_to_store_info ( val , min_compress_len ) <EOL> if not store_info : return ( <NUM_LIT:0> ) <EOL> fullcmd = "<STR_LIT>" % ( cmd , key , store_info [ <NUM_LIT:0> ] , time , store_info [ <NUM_LIT:1> ] , store_info [ <NUM_LIT:2> ] ) <EOL> try : <EOL> server . send_cmd ( fullcmd ) <EOL> return ( server . expect ( "<STR_LIT>" ) == "<STR_LIT>" ) <EOL> except socket . error , msg : <EOL> if type ( msg ) is types . TupleType : msg = msg [ <NUM_LIT:1> ] <EOL> server . mark_dead ( msg ) <EOL> return <NUM_LIT:0> <EOL> def get ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> check_key ( key ) <EOL> server , key = self . _get_server ( key ) <EOL> if not server : <EOL> return None <EOL> self . _statlog ( '<STR_LIT>' ) <EOL> try : <EOL> server . send_cmd ( "<STR_LIT>" % key ) <EOL> rkey , flags , rlen , = self . _expectvalue ( server ) <EOL> if not rkey : <EOL> return None <EOL> value = self . _recv_value ( server , flags , rlen ) <EOL> server . expect ( "<STR_LIT>" ) <EOL> except ( _Error , socket . error ) , msg : <EOL> if type ( msg ) is types . TupleType : msg = msg [ <NUM_LIT:1> ] <EOL> server . mark_dead ( msg ) <EOL> return None <EOL> return value <EOL> def get_multi ( self , keys , key_prefix = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> self . _statlog ( '<STR_LIT>' ) <EOL> server_keys , prefixed_to_orig_key = self . _map_and_prefix_keys ( keys , key_prefix ) <EOL> dead_servers = [ ] <EOL> for server in server_keys . iterkeys ( ) : <EOL> try : <EOL> server . send_cmd ( "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( server_keys [ server ] ) ) <EOL> except socket . error , msg : <EOL> if type ( msg ) is types . TupleType : msg = msg [ <NUM_LIT:1> ] <EOL> server . mark_dead ( msg ) <EOL> dead_servers . append ( server ) <EOL> for server in dead_servers : <EOL> del server_keys [ server ] <EOL> retvals = { } <EOL> for server in server_keys . iterkeys ( ) : <EOL> try : <EOL> line = server . readline ( ) <EOL> while line and line != '<STR_LIT>' : <EOL> rkey , flags , rlen = self . _expectvalue ( server , line ) <EOL> if rkey is not None : <EOL> val = self . _recv_value ( server , flags , rlen ) <EOL> retvals [ prefixed_to_orig_key [ rkey ] ] = val <EOL> line = server . readline ( ) <EOL> except ( _Error , socket . error ) , msg : <EOL> if type ( msg ) is types . TupleType : msg = msg [ <NUM_LIT:1> ] <EOL> server . mark_dead ( msg ) <EOL> return retvals <EOL> def _expectvalue ( self , server , line = None ) : <EOL> if not line : <EOL> line = server . readline ( ) <EOL> if line [ : <NUM_LIT:5> ] == '<STR_LIT>' : <EOL> resp , rkey , flags , len = line . split ( ) <EOL> flags = int ( flags ) <EOL> rlen = int ( len ) <EOL> return ( rkey , flags , rlen ) <EOL> else : <EOL> return ( None , None , None ) <EOL> def _recv_value ( self , server , flags , rlen ) : <EOL> rlen += <NUM_LIT:2> <EOL> buf = server . recv ( rlen ) <EOL> if len ( buf ) != rlen : <EOL> raise _Error ( "<STR_LIT>" % ( len ( buf ) , rlen ) ) <EOL> if len ( buf ) == rlen : <EOL> buf = buf [ : - <NUM_LIT:2> ] <EOL> if flags & Client . _FLAG_COMPRESSED : <EOL> buf = decompress ( buf ) <EOL> if flags == <NUM_LIT:0> or flags == Client . _FLAG_COMPRESSED : <EOL> val = buf <EOL> elif flags & Client . _FLAG_INTEGER : <EOL> val = int ( buf ) <EOL> elif flags & Client . _FLAG_LONG : <EOL> val = long ( buf ) <EOL> elif flags & Client . _FLAG_PICKLE : <EOL> try : <EOL> file = StringIO ( buf ) <EOL> unpickler = self . unpickler ( file ) <EOL> if self . persistent_load : <EOL> unpickler . persistent_load = self . persistent_load <EOL> val = unpickler . load ( ) <EOL> except Exception , e : <EOL> self . debuglog ( '<STR_LIT>' % e ) <EOL> val = None <EOL> else : <EOL> self . debuglog ( "<STR_LIT>" % flags ) <EOL> return val <EOL> class _Host : <EOL> _DEAD_RETRY = <NUM_LIT:30> <EOL> _SOCKET_TIMEOUT = <NUM_LIT:3> <EOL> def __init__ ( self , host , debugfunc = None ) : <EOL> if isinstance ( host , types . TupleType ) : <EOL> host , self . weight = host <EOL> else : <EOL> self . weight = <NUM_LIT:1> <EOL> m = re . match ( r'<STR_LIT>' , host ) <EOL> if not m : <EOL> m = re . match ( r'<STR_LIT>' <EOL> r'<STR_LIT>' , host ) <EOL> if not m : m = re . match ( r'<STR_LIT>' , host ) <EOL> if not m : <EOL> raise ValueError ( '<STR_LIT>' % host ) <EOL> hostData = m . groupdict ( ) <EOL> if hostData . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> self . family = socket . AF_UNIX <EOL> self . address = hostData [ '<STR_LIT:path>' ] <EOL> else : <EOL> self . family = socket . AF_INET <EOL> self . ip = hostData [ '<STR_LIT:host>' ] <EOL> self . port = int ( hostData . get ( '<STR_LIT:port>' , <NUM_LIT> ) ) <EOL> self . address = ( self . ip , self . port ) <EOL> if not debugfunc : <EOL> debugfunc = lambda x : x <EOL> self . debuglog = debugfunc <EOL> self . deaduntil = <NUM_LIT:0> <EOL> self . socket = None <EOL> self . buffer = '<STR_LIT>' <EOL> def _check_dead ( self ) : <EOL> if self . deaduntil and self . deaduntil > time . time ( ) : <EOL> return <NUM_LIT:1> <EOL> self . deaduntil = <NUM_LIT:0> <EOL> return <NUM_LIT:0> <EOL> def connect ( self ) : <EOL> if self . _get_socket ( ) : <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def mark_dead ( self , reason ) : <EOL> self . debuglog ( "<STR_LIT>" % ( self , reason ) ) <EOL> self . deaduntil = time . time ( ) + _Host . _DEAD_RETRY <EOL> self . close_socket ( ) <EOL> def _get_socket ( self ) : <EOL> if self . _check_dead ( ) : <EOL> return None <EOL> if self . socket : <EOL> return self . socket <EOL> s = socket . socket ( self . family , socket . SOCK_STREAM ) <EOL> if hasattr ( s , '<STR_LIT>' ) : s . settimeout ( self . _SOCKET_TIMEOUT ) <EOL> try : <EOL> s . connect ( self . address ) <EOL> except socket . timeout , msg : <EOL> self . mark_dead ( "<STR_LIT>" % msg ) <EOL> return None <EOL> except socket . error , msg : <EOL> if type ( msg ) is types . TupleType : msg = msg [ <NUM_LIT:1> ] <EOL> self . mark_dead ( "<STR_LIT>" % msg [ <NUM_LIT:1> ] ) <EOL> return None <EOL> self . socket = s <EOL> self . buffer = '<STR_LIT>' <EOL> return s <EOL> def close_socket ( self ) : <EOL> if self . socket : <EOL> self . socket . close ( ) <EOL> self . socket = None <EOL> def send_cmd ( self , cmd ) : <EOL> self . socket . sendall ( cmd + '<STR_LIT:\r\n>' ) <EOL> def send_cmds ( self , cmds ) : <EOL> """<STR_LIT>""" <EOL> self . socket . sendall ( cmds ) <EOL> def readline ( self ) : <EOL> buf = self . buffer <EOL> recv = self . socket . recv <EOL> while True : <EOL> index = buf . find ( '<STR_LIT:\r\n>' ) <EOL> if index >= <NUM_LIT:0> : <EOL> break <EOL> data = recv ( <NUM_LIT> ) <EOL> if not data : <EOL> self . mark_dead ( '<STR_LIT>' <EOL> % repr ( self ) ) <EOL> break <EOL> buf += data <EOL> if index >= <NUM_LIT:0> : <EOL> self . buffer = buf [ index + <NUM_LIT:2> : ] <EOL> buf = buf [ : index ] <EOL> else : <EOL> self . buffer = '<STR_LIT>' <EOL> return buf <EOL> def expect ( self , text ) : <EOL> line = self . readline ( ) <EOL> if line != text : <EOL> self . debuglog ( "<STR_LIT>" % ( text , line ) ) <EOL> return line <EOL> def recv ( self , rlen ) : <EOL> self_socket_recv = self . socket . recv <EOL> buf = self . buffer <EOL> while len ( buf ) < rlen : <EOL> foo = self_socket_recv ( <NUM_LIT> ) <EOL> buf += foo <EOL> if len ( foo ) == <NUM_LIT:0> : <EOL> raise _Error , ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( len ( buf ) , foo ) ) <EOL> self . buffer = buf [ rlen : ] <EOL> return buf [ : rlen ] <EOL> def __str__ ( self ) : <EOL> d = '<STR_LIT>' <EOL> if self . deaduntil : <EOL> d = "<STR_LIT>" % self . deaduntil <EOL> if self . family == socket . AF_INET : <EOL> return "<STR_LIT>" % ( self . address [ <NUM_LIT:0> ] , self . address [ <NUM_LIT:1> ] , d ) <EOL> else : <EOL> return "<STR_LIT>" % ( self . address , d ) <EOL> def check_key ( key , key_extra_len = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if type ( key ) == types . TupleType : key = key [ <NUM_LIT:1> ] <EOL> if not isinstance ( key , str ) : <EOL> raise Client . MemcachedStringEncodingError , ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if isinstance ( key , basestring ) : <EOL> if len ( key ) + key_extra_len > SERVER_MAX_KEY_LENGTH : <EOL> raise Client . MemcachedKeyLengthError , ( "<STR_LIT>" <EOL> % SERVER_MAX_KEY_LENGTH ) <EOL> for char in key : <EOL> if ord ( char ) < <NUM_LIT> or ord ( char ) == <NUM_LIT> : <EOL> raise Client . MemcachedKeyCharacterError , "<STR_LIT>" <EOL> def _doctest ( ) : <EOL> import doctest , memcache <EOL> servers = [ "<STR_LIT>" ] <EOL> mc = Client ( servers , debug = <NUM_LIT:1> ) <EOL> globs = { "<STR_LIT>" : mc } <EOL> return doctest . testmod ( memcache , globs = globs ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> print "<STR_LIT>" <EOL> _doctest ( ) <EOL> print "<STR_LIT>" <EOL> print <EOL> serverList = [ [ "<STR_LIT>" ] ] <EOL> if '<STR_LIT>' in sys . argv : <EOL> serverList . append ( [ os . path . join ( os . getcwd ( ) , '<STR_LIT>' ) ] ) <EOL> for servers in serverList : <EOL> mc = Client ( servers , debug = <NUM_LIT:1> ) <EOL> def to_s ( val ) : <EOL> if not isinstance ( val , types . StringTypes ) : <EOL> return "<STR_LIT>" % ( val , type ( val ) ) <EOL> return "<STR_LIT:%s>" % val <EOL> def test_setget ( key , val ) : <EOL> print "<STR_LIT>" % ( to_s ( key ) , to_s ( val ) ) , <EOL> mc . set ( key , val ) <EOL> newval = mc . get ( key ) <EOL> if newval == val : <EOL> print "<STR_LIT:OK>" <EOL> return <NUM_LIT:1> <EOL> else : <EOL> print "<STR_LIT>" <EOL> return <NUM_LIT:0> <EOL> class FooStruct : <EOL> def __init__ ( self ) : <EOL> self . bar = "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , FooStruct ) : <EOL> return self . bar == other . bar <EOL> return <NUM_LIT:0> <EOL> test_setget ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> test_setget ( "<STR_LIT>" , <NUM_LIT> ) <EOL> if test_setget ( "<STR_LIT>" , long ( <NUM_LIT:1> << <NUM_LIT:30> ) ) : <EOL> print "<STR_LIT>" , <EOL> if mc . delete ( "<STR_LIT>" ) : <EOL> print "<STR_LIT:OK>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , <EOL> print mc . get_multi ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> print "<STR_LIT>" , <EOL> print to_s ( mc . get ( "<STR_LIT>" ) ) <EOL> f = FooStruct ( ) <EOL> test_setget ( "<STR_LIT>" , f ) <EOL> print "<STR_LIT>" , <EOL> x = mc . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> if x == <NUM_LIT> : <EOL> print "<STR_LIT:OK>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , <EOL> x = mc . decr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> if x == <NUM_LIT> : <EOL> print "<STR_LIT:OK>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , <EOL> try : <EOL> x = mc . set ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> except Client . MemcachedKeyCharacterError , msg : <EOL> print "<STR_LIT:OK>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , <EOL> try : <EOL> x = mc . set ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> except Client . MemcachedKeyCharacterError , msg : <EOL> print "<STR_LIT:OK>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , <EOL> try : <EOL> x = mc . set ( '<STR_LIT:a>' * SERVER_MAX_KEY_LENGTH + '<STR_LIT>' , <NUM_LIT:1> ) <EOL> except Client . MemcachedKeyLengthError , msg : <EOL> print "<STR_LIT:OK>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , <EOL> try : <EOL> x = mc . set ( u'<STR_LIT>' , <NUM_LIT:1> ) <EOL> except Client . MemcachedStringEncodingError , msg : <EOL> print "<STR_LIT:OK>" , <EOL> else : <EOL> print "<STR_LIT>" , <EOL> try : <EOL> x = mc . set ( ( u'<STR_LIT:a>' * SERVER_MAX_KEY_LENGTH ) . encode ( '<STR_LIT:utf-8>' ) , <NUM_LIT:1> ) <EOL> except : <EOL> print "<STR_LIT>" , <EOL> else : <EOL> print "<STR_LIT:OK>" , <EOL> import pickle <EOL> s = pickle . loads ( '<STR_LIT>' ) <EOL> try : <EOL> x = mc . set ( ( s * SERVER_MAX_KEY_LENGTH ) . encode ( '<STR_LIT:utf-8>' ) , <NUM_LIT:1> ) <EOL> except Client . MemcachedKeyLengthError : <EOL> print "<STR_LIT:OK>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , <EOL> x = mc . set ( '<STR_LIT>' , '<STR_LIT:a>' * SERVER_MAX_VALUE_LENGTH ) <EOL> if mc . get ( '<STR_LIT>' ) == None : <EOL> print "<STR_LIT:OK>" , <EOL> else : <EOL> print "<STR_LIT>" , <EOL> x = mc . set ( '<STR_LIT>' , '<STR_LIT:a>' * SERVER_MAX_VALUE_LENGTH + '<STR_LIT>' ) <EOL> if mc . get ( '<STR_LIT>' ) == None : <EOL> print "<STR_LIT:OK>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , <EOL> mc . disconnect_all ( ) <EOL> errors = mc . set_multi ( { '<STR_LIT>' : '<STR_LIT:a>' , '<STR_LIT>' : '<STR_LIT:b>' } ) <EOL> if errors != [ ] : <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT:OK>" <EOL> print "<STR_LIT>" , <EOL> mc . disconnect_all ( ) <EOL> ret = mc . delete_multi ( { '<STR_LIT>' : '<STR_LIT:a>' , '<STR_LIT>' : '<STR_LIT:b>' } ) <EOL> if ret != <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT:OK>" </s>
