<s> from __future__ import print_function <EOL> from datetime import datetime <EOL> import socket <EOL> import sys <EOL> from cherrypy import wsgiserver <EOL> ALL_HOSTS = '<STR_LIT>' <EOL> DEFAULT_HOST = ALL_HOSTS <EOL> DEFAULT_PORT = <NUM_LIT> <EOL> MAX_PORT_DELTA = <NUM_LIT:10> <EOL> WELCOME = u'<STR_LIT>' <EOL> ADDRINUSE = u'<STR_LIT>' <EOL> RUNNING_ON = u'<STR_LIT>' <EOL> HOW_TO_QUIT = u'<STR_LIT>' <EOL> HTTPMSG = '<STR_LIT>' <EOL> class Server ( object ) : <EOL> def __init__ ( self , clay ) : <EOL> self . clay = clay <EOL> app = RequestLogger ( clay . app ) <EOL> self . dispatcher = wsgiserver . WSGIPathInfoDispatcher ( { '<STR_LIT:/>' : app } ) <EOL> def run ( self , host = DEFAULT_HOST , port = DEFAULT_PORT ) : <EOL> port = port or self . clay . settings . get ( '<STR_LIT:port>' , DEFAULT_PORT ) <EOL> host = host or self . clay . settings . get ( '<STR_LIT:host>' , DEFAULT_HOST ) <EOL> max_port = port + MAX_PORT_DELTA <EOL> print ( WELCOME ) <EOL> return self . _testrun ( host , port , max_port ) <EOL> def _testrun ( self , host , current_port , max_port ) : <EOL> self . print_help_msg ( host , current_port ) <EOL> try : <EOL> self . _run_wsgi_server ( host , current_port ) <EOL> except socket . error : <EOL> current_port += <NUM_LIT:1> <EOL> if current_port > max_port : <EOL> return <EOL> print ( ADDRINUSE ) <EOL> self . _testrun ( host , current_port , max_port ) <EOL> except KeyboardInterrupt : <EOL> self . stop ( ) <EOL> return host , current_port <EOL> def _run_wsgi_server ( self , host , port ) : <EOL> self . server = self . _get_wsgi_server ( host , port ) <EOL> self . start ( ) <EOL> def _get_wsgi_server ( self , host , port ) : <EOL> return wsgiserver . CherryPyWSGIServer ( <EOL> ( host , port ) , <EOL> wsgi_app = self . dispatcher <EOL> ) <EOL> def start ( self ) : <EOL> self . server . start ( ) <EOL> def stop ( self ) : <EOL> self . server . stop ( ) <EOL> def print_help_msg ( self , host , port ) : <EOL> if host == ALL_HOSTS : <EOL> print ( RUNNING_ON % ( '<STR_LIT:localhost>' , port ) ) <EOL> local_ip = get_local_ip ( ) <EOL> if local_ip : <EOL> print ( RUNNING_ON % ( local_ip , port ) ) <EOL> print ( HOW_TO_QUIT ) <EOL> class RequestLogger ( object ) : <EOL> def __init__ ( self , application , ** kw ) : <EOL> self . application = application <EOL> def log_request ( self , environ , now = None ) : <EOL> now = now or datetime . now ( ) <EOL> msg = [ <EOL> '<STR_LIT:U+0020>' , <EOL> now . strftime ( '<STR_LIT>' ) , '<STR_LIT>' , <EOL> environ . get ( '<STR_LIT>' , '<STR_LIT:?>' ) , '<STR_LIT:U+0020>' , <EOL> environ . get ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:U+0020>' , <EOL> '<STR_LIT:(>' , environ . get ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:)>' , <EOL> ] <EOL> msg = '<STR_LIT>' . join ( msg ) <EOL> print ( msg ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> self . log_request ( environ ) <EOL> try : <EOL> return self . application ( environ , start_response ) <EOL> except Exception : <EOL> start_response ( <EOL> HTTPMSG , <EOL> [ ( '<STR_LIT>' , '<STR_LIT>' ) ] , <EOL> sys . exc_info ( ) <EOL> ) <EOL> raise <EOL> def get_local_ip ( ) : <EOL> try : <EOL> interfaces = socket . gethostbyname_ex ( socket . gethostname ( ) ) [ - <NUM_LIT:1> ] <EOL> except socket . gaierror : <EOL> return <EOL> for ip in interfaces : <EOL> if ip . startswith ( '<STR_LIT>' ) : <EOL> return ip </s>
