class Service_Unavailable (Server_Error) : """The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. """ status_code = 503 retry_after = CAL.Date_Time_Delta (minutes = 3) _spec = \ """ The implication is that this is a temporary condition which will be alleviated after some delay. If known, the length of the delay MAY be indicated in a Retry-After header. If no Retry-After is given, the client SHOULD handle the response as it would for a 500 response. Note: The existence of the 503 status code does not imply that a server must use it when becoming overloaded. Some servers may wish to simply refuse the connection. """ def _add_response_headers (self, resource, request, response) : self.__super._add_response_headers (resource, request, response) try : retry_after = self.retry_after except AttributeError : pass else : if isinstance (retry_after, CAL.Date_Time_Delta) : retry_after = CAL.Date_Time ().as_utc () + retry_after if retry_after is not None : response.set_header ("Retry-After", retry_after)
class GTW_Command(MOM.Command): _rn_prefix = "GTW" ANS = GTW _defaults = dict \ ( edit_session_ttl = CAL.Date_Time_Delta (hours = 3) , input_encoding = "utf-8" , languages = "en,de" , local_code = "en_US" , media_domain = None , output_encoding = "utf-8" , template_file = "html/static.jnj" , user_session_ttl = CAL.Date_Time_Delta (days = 3) ) ### Sub-commands defined as class attributes to allow redefinition by ### derived classes; meta class puts their names into `_sub_commands` class _GTW_Server_Base_(_Sub_Command_): ### Base for server-related commands is_partial = True _opts = \ ( "-auto_reload:B" "?Autoload of werkzeug, only works with no sqlite db" , "-Break:B?Enter debugger right after creating wsgi app" , "-debug:B=no" , "-email_from:S" "?Email address to use as from-address for emails sent " "by the app" , "-languages:T,?Languages for which to load translations" , "-locale_code:S?Code of locale to use" , "-media_domain:S?Serve media content from the domain specified" , "-serve_static_files:B?Serve static files" , "-smtp_server:S=localhost?SMTP server used to send emails" , "-template_file:S" , "-TEST:B" , HTTP_Opt (default = "Werkzeug") , TFL.CAO.Opt.Date_Time_Delta ( name = "edit_session_ttl" , description = "Time to live for edit session" ) , TFL.CAO.Opt.Input_Encoding ( description = "Default encoding for source files" ) , TFL.CAO.Opt.Output_Encoding ( description = "Default encoding for generated html" ) , TFL.CAO.Opt.Time_Zone () , TFL.CAO.Opt.Date_Time_Delta ( name = "user_session_ttl" , description = "Time to live for user session (cookie)" ) ) _Server_Base_ = _GTW_Server_Base_ # end class class _GTW_FCGI_(_GTW_Server_Base_): """Run as a FastCGI server.""" _FCGI_ = _GTW_FCGI_ # end class class _GTW_Run_Server_(_GTW_Server_Base_): """Run as application server.""" _defaults = dict \ ( auto_reload = True , serve_static_files = True ) _Run_Server_ = _GTW_Run_Server_ # end class class _GTW_Setup_Cache_(_GTW_Server_Base_): """Setup the cache of the application.""" _Setup_Cache_ = _GTW_Setup_Cache_ # end class class _GTW_Script_(_GTW_Server_Base_, MOM.Command._Script_): _opts = \ ( "wsgi:B?Create the wsgi application before running the script(s)" , ) _Script_ = _GTW_Script_ # end class class _GTW_Shell_(_GTW_Server_Base_, MOM.Command._Shell_): _opts = \ ( "wsgi:B?Create the wsgi application before entering the shell" , ) _Shell_ = _GTW_Shell_ # end class class _GTW_WSGI_(_GTW_Server_Base_): """Run as wsgi application.""" _WSGI_ = _GTW_WSGI_ # end class @property def now(self): return datetime.datetime.now().replace(microsecond=0) # end def now def _handle_fcgi(self, cao): from flup.server.fcgi import WSGIServer start = self.now exe = "%s fcgi" % sos.path.abspath(self.app_path) if cao.log_level: logging.info("[%s] Starting %s" % (start, exe)) try: return WSGIServer(self._handle_wsgi(cao)).run() finally: if cao.log_level: logging.info \ ("[%s <-- %s] Finished %s" % (self.now, start, exe)) # end def _handle_fcgi def _handle_run_server(self, cao): raise NotImplementedError # end def _handle_run_server def _handle_setup_cache(self, cao): raise NotImplementedError # end def _handle_setup_cache def _handle_script_globals(self, cao, scope, **kw): if cao.wsgi: wsgi = self._handle_wsgi(cao) root = top = self.root kw.update(root=root, top=top, wsgi=wsgi) return self.__super._handle_script_globals \ ( cao, scope , CAL = CAL , GTW = GTW , ** kw ) # end def _handle_script_globals def _handle_wsgi(self, cao): raise NotImplementedError