<s> VERSION_STRING = '<STR_LIT>' <EOL> import coro <EOL> import socket <EOL> import string <EOL> import cStringIO <EOL> import sys <EOL> import traceback <EOL> import types <EOL> import os <EOL> class backdoor : <EOL> def __init__ ( self , socket , line_separator = '<STR_LIT:\r\n>' , welcome_message = None , global_dict = None ) : <EOL> self . socket = socket <EOL> self . buffer = '<STR_LIT>' <EOL> self . lines = [ ] <EOL> self . multilines = [ ] <EOL> self . line_separator = line_separator <EOL> self . welcome_message = welcome_message <EOL> self . global_dict = global_dict <EOL> if '<STR_LIT>' not in sys . __dict__ : <EOL> sys . ps1 = '<STR_LIT>' <EOL> if '<STR_LIT>' not in sys . __dict__ : <EOL> sys . ps2 = '<STR_LIT>' <EOL> def send ( self , data ) : <EOL> try : <EOL> self . socket . send ( data ) <EOL> except : <EOL> pass <EOL> def prompt ( self ) : <EOL> if self . multilines : <EOL> self . send ( sys . ps2 ) <EOL> else : <EOL> self . send ( sys . ps1 ) <EOL> def read_line ( self ) : <EOL> if self . lines : <EOL> l = self . lines [ <NUM_LIT:0> ] <EOL> self . lines = self . lines [ <NUM_LIT:1> : ] <EOL> return l <EOL> else : <EOL> while not self . lines : <EOL> block = self . socket . recv ( <NUM_LIT> ) <EOL> if not block : <EOL> return None <EOL> elif block == '<STR_LIT>' : <EOL> self . socket . close ( ) <EOL> return None <EOL> else : <EOL> self . buffer = self . buffer + block <EOL> lines = string . split ( self . buffer , self . line_separator ) <EOL> for l in lines [ : - <NUM_LIT:1> ] : <EOL> self . lines . append ( l ) <EOL> self . buffer = lines [ - <NUM_LIT:1> ] <EOL> return self . read_line ( ) <EOL> def send_welcome_message ( self ) : <EOL> self . send ( '<STR_LIT>' + sys . version + self . line_separator ) <EOL> self . send ( sys . copyright + self . line_separator ) <EOL> if self . welcome_message is not None : <EOL> lines = string . split ( self . welcome_message , '<STR_LIT:\n>' ) <EOL> if lines [ - <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> lines . append ( '<STR_LIT>' ) <EOL> self . send ( string . join ( lines , self . line_separator ) ) <EOL> def read_eval_print_loop ( self ) : <EOL> self . send_welcome_message ( ) <EOL> if self . global_dict is None : <EOL> env = sys . modules [ '<STR_LIT:__main__>' ] . __dict__ . copy ( ) <EOL> else : <EOL> env = self . global_dict . copy ( ) <EOL> for name , value in env . items ( ) : <EOL> if ( name . startswith ( '<STR_LIT>' ) and <EOL> name . endswith ( '<STR_LIT>' ) and <EOL> name not in ( '<STR_LIT>' , ) ) : <EOL> del env [ name ] <EOL> env [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> while True : <EOL> self . prompt ( ) <EOL> line = self . read_line ( ) <EOL> if line is None : <EOL> break <EOL> elif self . multilines : <EOL> self . multilines . append ( line ) <EOL> if line == '<STR_LIT>' : <EOL> code = string . join ( self . multilines , '<STR_LIT:\n>' ) <EOL> self . parse ( code , env ) <EOL> self . multilines = [ ] <EOL> else : <EOL> self . parse ( line , env ) <EOL> def parse ( self , line , env ) : <EOL> save = sys . stdout , sys . stderr <EOL> output = cStringIO . StringIO ( ) <EOL> try : <EOL> try : <EOL> sys . stdout = sys . stderr = output <EOL> co = compile ( line , repr ( self ) , '<STR_LIT>' ) <EOL> result = eval ( co , env ) <EOL> if result is not None : <EOL> print repr ( result ) <EOL> env [ '<STR_LIT:_>' ] = result <EOL> except SyntaxError : <EOL> try : <EOL> co = compile ( line , repr ( self ) , '<STR_LIT>' ) <EOL> exec co in env <EOL> except SyntaxError as msg : <EOL> if not self . multilines and msg [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> self . multilines . append ( line ) <EOL> else : <EOL> traceback . print_exc ( ) <EOL> except : <EOL> traceback . print_exc ( ) <EOL> except : <EOL> traceback . print_exc ( ) <EOL> finally : <EOL> sys . stdout , sys . stderr = save <EOL> self . send ( output . getvalue ( ) ) <EOL> del output <EOL> def client ( conn , addr , welcome_message = None , global_dict = None ) : <EOL> b = backdoor ( conn , welcome_message = welcome_message , global_dict = global_dict ) <EOL> b . read_eval_print_loop ( ) <EOL> def serve ( port = None , ip = '<STR_LIT:127.0.0.1>' , unix_path = None , welcome_message = None , global_dict = None ) : <EOL> import errno <EOL> if unix_path : <EOL> try : <EOL> os . remove ( unix_path ) <EOL> except OSError as why : <EOL> if why [ <NUM_LIT:0> ] == errno . ENOENT : <EOL> pass <EOL> else : <EOL> raise <EOL> try : <EOL> s = coro . make_socket ( socket . AF_UNIX , socket . SOCK_STREAM ) <EOL> s . bind ( unix_path ) <EOL> except OSError : <EOL> coro . print_stderr ( '<STR_LIT>' % unix_path ) <EOL> raise <EOL> coro . print_stderr ( '<STR_LIT>' % unix_path ) <EOL> else : <EOL> s = coro . make_socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> s . set_reuse_addr ( ) <EOL> if port is None : <EOL> ports = xrange ( <NUM_LIT> , <NUM_LIT> ) <EOL> else : <EOL> if isinstance ( port , types . IntType ) : <EOL> ports = [ port ] <EOL> else : <EOL> ports = port <EOL> bound = <NUM_LIT:0> <EOL> for i in ports : <EOL> try : <EOL> s . bind ( ( ip , i ) ) <EOL> bound = <NUM_LIT:1> <EOL> break <EOL> except ( OSError , socket . error ) as why : <EOL> if why [ <NUM_LIT:0> ] != errno . EADDRINUSE : <EOL> raise OSError ( why ) <EOL> if not bound : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> coro . print_stderr ( '<STR_LIT>' % i ) <EOL> s . listen ( <NUM_LIT> ) <EOL> while True : <EOL> conn , addr = s . accept ( ) <EOL> coro . print_stderr ( '<STR_LIT>' % ( conn . getsockname ( ) , ) ) <EOL> thread = coro . spawn ( client , conn , addr , welcome_message , global_dict ) <EOL> thread . set_name ( '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> thread = coro . spawn ( serve , welcome_message = '<STR_LIT>' ) <EOL> thread . set_name ( '<STR_LIT>' ) <EOL> coro . event_loop ( <NUM_LIT> ) </s>
