def main(): # Required so PDB prompts work properly. Originally tried to disable buffering # (both by adding the -u flag when starting this process and by adding # "stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)" but neither worked). sys.stdout = AutoFlush(sys.stdout) assert len(sys.argv) == 3 child_in_path = sys.argv[1] child_out_path = sys.argv[2] config = runtime_config_pb2.Config() config.ParseFromString(open(child_in_path, 'rb').read()) os.remove(child_in_path) child_out = open(child_out_path, 'wb') debugging_app = None if config.python_config and config.python_config.startup_script: global_vars = {'config': config} try: exec( compile( open(config.python_config.startup_script).read(), config.python_config.startup_script, 'exec'), global_vars) except Exception as e: debugging_app = StartupScriptFailureApplication( config, str(e), traceback.format_exc()) if debugging_app: server = wsgi_server.WsgiServer(('localhost', 0), debugging_app) else: setup_stubs(config) sandbox.enable_sandbox(config) # This import needs to be after enabling the sandbox so the runtime # implementation imports the sandboxed version of the logging module. from google.appengine.tools.devappserver2.python import request_handler server = wsgi_server.WsgiServer( ('localhost', 0), request_rewriter.runtime_rewriter_middleware( request_handler.RequestHandler(config))) server.start() child_out.write('{}\n'.format(server.port).encode()) child_out.close() try: while True: time.sleep(1) except KeyboardInterrupt: pass finally: server.quit()
# This line needs to be before enabling the sandbox because os.environ is # patched away. port = os.environ['PORT'] if debugging_app: server = wsgi_server.WsgiServer( ('localhost', port), debugging_app) else: setup_stubs(config) sandbox.enable_sandbox(config) # This import needs to be after enabling the sandbox so the runtime # implementation imports the sandboxed version of the logging module. from google.appengine.tools.devappserver2.python import request_handler server = wsgi_server.WsgiServer( ('localhost', port), request_rewriter.runtime_rewriter_middleware( request_handler.RequestHandler(config))) server.start() try: while True: time.sleep(1) except KeyboardInterrupt: pass finally: server.quit() if __name__ == '__main__': main()