<s> """<STR_LIT>""" <EOL> __version__ = "<STR_LIT>" <EOL> import socket <EOL> import sys <EOL> import os <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> if hasattr ( socket , "<STR_LIT>" ) : <EOL> __all__ . extend ( [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> class BaseServer : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , server_address , RequestHandlerClass ) : <EOL> """<STR_LIT>""" <EOL> self . server_address = server_address <EOL> self . RequestHandlerClass = RequestHandlerClass <EOL> def server_activate ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def serve_forever ( self ) : <EOL> """<STR_LIT>""" <EOL> while <NUM_LIT:1> : <EOL> self . handle_request ( ) <EOL> def handle_request ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> request , client_address = self . get_request ( ) <EOL> except socket . error : <EOL> return <EOL> if self . verify_request ( request , client_address ) : <EOL> try : <EOL> self . process_request ( request , client_address ) <EOL> except : <EOL> self . handle_error ( request , client_address ) <EOL> self . close_request ( request ) <EOL> def verify_request ( self , request , client_address ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def process_request ( self , request , client_address ) : <EOL> """<STR_LIT>""" <EOL> self . finish_request ( request , client_address ) <EOL> self . close_request ( request ) <EOL> def server_close ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def finish_request ( self , request , client_address ) : <EOL> """<STR_LIT>""" <EOL> self . RequestHandlerClass ( request , client_address , self ) <EOL> def close_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def handle_error ( self , request , client_address ) : <EOL> """<STR_LIT>""" <EOL> print '<STR_LIT:->' * <NUM_LIT> <EOL> print '<STR_LIT>' , <EOL> print client_address <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> print '<STR_LIT:->' * <NUM_LIT> <EOL> class TCPServer ( BaseServer ) : <EOL> """<STR_LIT>""" <EOL> address_family = socket . AF_INET <EOL> socket_type = socket . SOCK_STREAM <EOL> request_queue_size = <NUM_LIT:5> <EOL> allow_reuse_address = False <EOL> def __init__ ( self , server_address , RequestHandlerClass ) : <EOL> """<STR_LIT>""" <EOL> BaseServer . __init__ ( self , server_address , RequestHandlerClass ) <EOL> self . socket = socket . socket ( self . address_family , <EOL> self . socket_type ) <EOL> self . server_bind ( ) <EOL> self . server_activate ( ) <EOL> def server_bind ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . allow_reuse_address : <EOL> self . socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> self . socket . bind ( self . server_address ) <EOL> self . server_address = self . socket . getsockname ( ) <EOL> def server_activate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . socket . listen ( self . request_queue_size ) <EOL> self . server_address = self . socket . getsockname ( ) <EOL> def server_close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . socket . close ( ) <EOL> def fileno ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . socket . fileno ( ) <EOL> def get_request ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . socket . accept ( ) <EOL> def close_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> request . close ( ) <EOL> class UDPServer ( TCPServer ) : <EOL> """<STR_LIT>""" <EOL> allow_reuse_address = False <EOL> socket_type = socket . SOCK_DGRAM <EOL> max_packet_size = <NUM_LIT> <EOL> def get_request ( self ) : <EOL> data , client_addr = self . socket . recvfrom ( self . max_packet_size ) <EOL> return ( data , self . socket ) , client_addr <EOL> def server_activate ( self ) : <EOL> pass <EOL> def close_request ( self , request ) : <EOL> pass <EOL> class ForkingMixIn : <EOL> """<STR_LIT>""" <EOL> active_children = None <EOL> max_children = <NUM_LIT> <EOL> def collect_children ( self ) : <EOL> """<STR_LIT>""" <EOL> while self . active_children : <EOL> if len ( self . active_children ) < self . max_children : <EOL> options = os . WNOHANG <EOL> else : <EOL> options = <NUM_LIT:0> <EOL> try : <EOL> pid , status = os . waitpid ( <NUM_LIT:0> , options ) <EOL> except os . error : <EOL> pid = None <EOL> if not pid : break <EOL> self . active_children . remove ( pid ) <EOL> def process_request ( self , request , client_address ) : <EOL> """<STR_LIT>""" <EOL> self . collect_children ( ) <EOL> pid = os . fork ( ) <EOL> if pid : <EOL> if self . active_children is None : <EOL> self . active_children = [ ] <EOL> self . active_children . append ( pid ) <EOL> self . close_request ( request ) <EOL> return <EOL> else : <EOL> try : <EOL> self . finish_request ( request , client_address ) <EOL> os . _exit ( <NUM_LIT:0> ) <EOL> except : <EOL> try : <EOL> self . handle_error ( request , client_address ) <EOL> finally : <EOL> os . _exit ( <NUM_LIT:1> ) <EOL> class ThreadingMixIn : <EOL> """<STR_LIT>""" <EOL> daemon_threads = False <EOL> def process_request_thread ( self , request , client_address ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . finish_request ( request , client_address ) <EOL> self . close_request ( request ) <EOL> except : <EOL> self . handle_error ( request , client_address ) <EOL> self . close_request ( request ) <EOL> def process_request ( self , request , client_address ) : <EOL> """<STR_LIT>""" <EOL> import threading <EOL> t = threading . Thread ( target = self . process_request_thread , <EOL> args = ( request , client_address ) ) <EOL> if self . daemon_threads : <EOL> t . setDaemon ( <NUM_LIT:1> ) <EOL> t . start ( ) <EOL> class ForkingUDPServer ( ForkingMixIn , UDPServer ) : pass <EOL> class ForkingTCPServer ( ForkingMixIn , TCPServer ) : pass <EOL> class ThreadingUDPServer ( ThreadingMixIn , UDPServer ) : pass <EOL> class ThreadingTCPServer ( ThreadingMixIn , TCPServer ) : pass <EOL> if hasattr ( socket , '<STR_LIT>' ) : <EOL> class UnixStreamServer ( TCPServer ) : <EOL> address_family = socket . AF_UNIX <EOL> class UnixDatagramServer ( UDPServer ) : <EOL> address_family = socket . AF_UNIX <EOL> class ThreadingUnixStreamServer ( ThreadingMixIn , UnixStreamServer ) : pass <EOL> class ThreadingUnixDatagramServer ( ThreadingMixIn , UnixDatagramServer ) : pass <EOL> class BaseRequestHandler : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request , client_address , server ) : <EOL> self . request = request <EOL> self . client_address = client_address <EOL> self . server = server <EOL> self . setup ( ) <EOL> try : <EOL> self . handle ( ) <EOL> finally : <EOL> self . finish ( ) <EOL> def setup ( self ) : <EOL> pass <EOL> def handle ( self ) : <EOL> pass <EOL> def finish ( self ) : <EOL> pass <EOL> class StreamRequestHandler ( BaseRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> rbufsize = - <NUM_LIT:1> <EOL> wbufsize = <NUM_LIT:0> <EOL> def setup ( self ) : <EOL> self . connection = self . request <EOL> self . rfile = self . connection . makefile ( '<STR_LIT:rb>' , self . rbufsize ) <EOL> self . wfile = self . connection . makefile ( '<STR_LIT:wb>' , self . wbufsize ) <EOL> def finish ( self ) : <EOL> if not self . wfile . closed : <EOL> self . wfile . flush ( ) <EOL> self . wfile . close ( ) <EOL> self . rfile . close ( ) <EOL> class DatagramRequestHandler ( BaseRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def setup ( self ) : <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> self . packet , self . socket = self . request <EOL> self . rfile = StringIO ( self . packet ) <EOL> self . wfile = StringIO ( ) <EOL> def finish ( self ) : <EOL> self . socket . sendto ( self . wfile . getvalue ( ) , self . client_address ) </s>
