<s> import secure_smtpd <EOL> import ssl , smtpd , asyncore , socket , logging , signal , time , sys <EOL> from . smtp_channel import SMTPChannel <EOL> from asyncore import ExitNow <EOL> from . process_pool import ProcessPool <EOL> from ssl import SSLError <EOL> try : <EOL> from Queue import Empty <EOL> except ImportError : <EOL> from queue import Empty <EOL> class SMTPServer ( smtpd . SMTPServer ) : <EOL> def __init__ ( self , localaddr , remoteaddr , ssl = False , certfile = None , keyfile = None , ssl_version = ssl . PROTOCOL_SSLv23 , require_authentication = False , credential_validator = None , maximum_execution_time = <NUM_LIT:30> , process_count = <NUM_LIT:5> ) : <EOL> smtpd . SMTPServer . __init__ ( self , localaddr , remoteaddr ) <EOL> self . logger = logging . getLogger ( secure_smtpd . LOG_NAME ) <EOL> self . certfile = certfile <EOL> self . keyfile = keyfile <EOL> self . ssl_version = ssl_version <EOL> self . subprocesses = [ ] <EOL> self . require_authentication = require_authentication <EOL> self . credential_validator = credential_validator <EOL> self . ssl = ssl <EOL> self . maximum_execution_time = maximum_execution_time <EOL> self . process_count = process_count <EOL> self . process_pool = None <EOL> def handle_accept ( self ) : <EOL> self . process_pool = ProcessPool ( self . _accept_subprocess , process_count = self . process_count ) <EOL> self . close ( ) <EOL> def _accept_subprocess ( self , queue ) : <EOL> while True : <EOL> try : <EOL> self . socket . setblocking ( <NUM_LIT:1> ) <EOL> pair = self . accept ( ) <EOL> map = { } <EOL> if pair is not None : <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> newsocket , fromaddr = pair <EOL> newsocket . settimeout ( self . maximum_execution_time ) <EOL> if self . ssl : <EOL> newsocket = ssl . wrap_socket ( <EOL> newsocket , <EOL> server_side = True , <EOL> certfile = self . certfile , <EOL> keyfile = self . keyfile , <EOL> ssl_version = self . ssl_version , <EOL> ) <EOL> channel = SMTPChannel ( <EOL> self , <EOL> newsocket , <EOL> fromaddr , <EOL> require_authentication = self . require_authentication , <EOL> credential_validator = self . credential_validator , <EOL> map = map <EOL> ) <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> asyncore . loop ( map = map ) <EOL> self . logger . error ( '<STR_LIT>' ) <EOL> except ( ExitNow , SSLError ) : <EOL> self . _shutdown_socket ( newsocket ) <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> except Exception as e : <EOL> self . _shutdown_socket ( newsocket ) <EOL> self . logger . error ( '<STR_LIT>' % str ( e ) ) <EOL> def _shutdown_socket ( self , s ) : <EOL> try : <EOL> s . shutdown ( socket . SHUT_RDWR ) <EOL> s . close ( ) <EOL> except Exception as e : <EOL> self . logger . error ( '<STR_LIT>' % str ( e ) ) <EOL> def run ( self ) : <EOL> asyncore . loop ( ) <EOL> if hasattr ( signal , '<STR_LIT>' ) : <EOL> def sig_handler ( signal , frame ) : <EOL> self . logger . info ( "<STR_LIT>" % signal ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> signal . signal ( signal . SIGTERM , sig_handler ) <EOL> while <NUM_LIT:1> : <EOL> time . sleep ( <NUM_LIT:1> ) </s>
