class AdminMediaHandlerTests(TestCase): def setUp(self): self.admin_media_url = urljoin(settings.STATIC_URL, 'admin/') self.admin_media_file_path = os.path.abspath( os.path.join(django.__path__[0], 'contrib', 'admin', 'static', 'admin') ) self.handler = AdminMediaHandler(WSGIHandler()) def test_media_urls(self): """ Tests that URLs that look like absolute file paths after the settings.STATIC_URL don't turn into absolute file paths. """ # Cases that should work on all platforms. data = ( ('%scss/base.css' % self.admin_media_url, ('css', 'base.css')), ) # Cases that should raise an exception. bad_data = () # Add platform-specific cases. if os.sep == '/': data += ( # URL, tuple of relative path parts. ('%s\\css/base.css' % self.admin_media_url, ('\\css', 'base.css')), ) bad_data += ( '%s/css/base.css' % self.admin_media_url, '%s///css/base.css' % self.admin_media_url, '%s../css/base.css' % self.admin_media_url, ) elif os.sep == '\\': bad_data += ( '%sC:\css/base.css' % self.admin_media_url, '%s/\\css/base.css' % self.admin_media_url, '%s\\css/base.css' % self.admin_media_url, '%s\\\\css/base.css' % self.admin_media_url ) for url, path_tuple in data: try: output = self.handler.file_path(url) except ValueError: self.fail("Got a ValueError exception, but wasn't expecting" " one. URL was: %s" % url) rel_path = os.path.join(*path_tuple) desired = os.path.normcase( os.path.join(self.admin_media_file_path, rel_path)) self.assertEqual(output, desired, "Got: %s, Expected: %s, URL was: %s" % (output, desired, url)) for url in bad_data: try: output = self.handler.file_path(url) except ValueError: continue self.fail('URL: %s should have caused a ValueError exception.' % url)
def main(): # Check for the database database_file = mathics_settings.DATABASES['default']['NAME'] if not os.path.exists(database_file): print "Error: Mathics database not found!" print "Please change to the mathics install directory and run:\n" print " $> python setup.py initialize\n" print "as the current user" sys.exit(-1) os.environ['DJANGO_SETTINGS_MODULE'] = 'mathics.settings' #os.putenv('DJANGO_SETTINGS_MODULE', 'mathics.settings') from django.conf import settings from django.core.servers.basehttp import run, WSGIServerException from django.core.handlers.wsgi import WSGIHandler argparser = argparse.ArgumentParser( prog='mathicsserver', usage='%(prog)s [options]', add_help=False, description="""Mathics server for the graphical user interface in a web browser. It is not intended for production use on a public Web server!""", epilog="""Please feel encouraged to contribute to Mathics! Create your own fork, make the desired changes, commit, and make a pull request.""") argparser.add_argument('--help', '-h', help='show this help message and exit', action='help') argparser.add_argument('--quiet', '-q', help='don\'t print message at startup', action='store_true') argparser.add_argument('--version', '-v', action='version', version='%(prog)s ' + settings.VERSION) argparser.add_argument("--port", "-p", dest="port", metavar="PORT", default=8000, type=int, help="use PORT as server port") argparser.add_argument("--external", "-e", dest="external", action="store_true", help="allow external access to server") args = argparser.parse_args() quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C' port = args.port if not args.quiet: print_version(is_server=True) print_license() print u"Quit by pressing %s\n" % quit_command print u"Open the graphical user interface at\nhttp://localhost:%d\nin Firefox, Chrome, or Safari to use Mathics\n" % port if args.external: addr = '0.0.0.0' else: addr = '' try: if settings.DJANGO_VERSION < (1, 4): from django.core.servers.basehttp import AdminMediaHandler handler = AdminMediaHandler(WSGIHandler(), '') else: from django.core.servers.basehttp import get_internal_wsgi_application handler = get_internal_wsgi_application() run(addr, port, handler) except WSGIServerException, e: # Use helpful error messages instead of ugly tracebacks. ERRORS = { 13: "You don't have permission to access that port.", 98: "That port is already in use.", 99: "That IP address can't be assigned to.", } try: error_text = ERRORS[e.args[0].args[0]] except (AttributeError, KeyError): error_text = str(e) sys.stderr.write("Error: %s" % error_text + '\n') # Need to use an OS exit because sys.exit doesn't work in a thread os._exit(1)
def inner_run(): if self.show_startup_messages: print("Performing system checks...\n") if hasattr(self, 'check'): self.check(display_num_errors=self.show_startup_messages) else: self.validate(display_num_errors=self.show_startup_messages) if HAS_MIGRATIONS: try: self.check_migrations() except ImproperlyConfigured: pass if self.show_startup_messages: print("\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)) print("Development server is running at %s" % (bind_url, )) print( "Using the Werkzeug debugger (http://werkzeug.pocoo.org/)") print("Quit the server with %s." % quit_command) path = options.get('admin_media_path', '') if not path: admin_media_path = os.path.join(django.__path__[0], 'contrib/admin/static/admin') if os.path.isdir(admin_media_path): path = admin_media_path else: path = os.path.join(django.__path__[0], 'contrib/admin/media') handler = WSGIHandler() if USE_ADMINMEDIAHANDLER: handler = AdminMediaHandler(handler, path) if USE_STATICFILES: use_static_handler = options.get('use_static_handler', True) insecure_serving = options.get('insecure_serving', False) if use_static_handler and (settings.DEBUG or insecure_serving): handler = StaticFilesHandler(handler) if open_browser: import webbrowser webbrowser.open(bind_url) if cert_path: """ OpenSSL is needed for SSL support. This will make flakes8 throw warning since OpenSSL is not used directly, alas, this is the only way to show meaningful error messages. See: http://lucumr.pocoo.org/2011/9/21/python-import-blackbox/ for more information on python imports. """ try: import OpenSSL # NOQA except ImportError: raise CommandError( "Python OpenSSL Library is " "required to use runserver_plus with ssl support. " "Install via pip (pip install pyOpenSSL).") dir_path, cert_file = os.path.split(cert_path) if not dir_path: dir_path = os.getcwd() root, ext = os.path.splitext(cert_file) certfile = os.path.join(dir_path, root + ".crt") keyfile = os.path.join(dir_path, root + ".key") try: from werkzeug.serving import make_ssl_devcert if os.path.exists(certfile) and \ os.path.exists(keyfile): ssl_context = (certfile, keyfile) else: # Create cert, key files ourselves. ssl_context = make_ssl_devcert(os.path.join( dir_path, root), host='localhost') except ImportError: if self.show_startup_messages: print( "Werkzeug version is less than 0.9, trying adhoc certificate." ) ssl_context = "adhoc" else: ssl_context = None if use_reloader and settings.USE_I18N: try: from django.utils.autoreload import gen_filenames except ImportError: pass else: extra_files.extend( filter(lambda filename: filename.endswith('.mo'), gen_filenames())) run_simple( self.addr, int(self.port), DebuggedApplication(handler, True), use_reloader=use_reloader, use_debugger=True, extra_files=extra_files, reloader_interval=reloader_interval, threaded=threaded, ssl_context=ssl_context, )
def main(): # Check for the database database_file = mathics_settings.DATABASES['default']['NAME'] if not os.path.exists(database_file): print("warning: database file %s not found\n" % database_file) import subprocess if not os.path.exists(mathics_settings.DATA_DIR): print("Creating data directory %s" % mathics_settings.DATA_DIR) os.makedirs(mathics_settings.DATA_DIR) print("Creating database %s" % database_file) manage_file = os.path.join( os.path.dirname(os.path.realpath(__file__)), "manage.py") try: subprocess.check_call( [sys.executable, manage_file, 'syncdb', '--noinput']) print("\ndatabase initialized sucessfully") except subprocess.CalledProcessError: print("error: failed to create database") sys.exit(1) os.environ['DJANGO_SETTINGS_MODULE'] = 'mathics.settings' # os.putenv('DJANGO_SETTINGS_MODULE', 'mathics.settings') from django.conf import settings from django.core.servers.basehttp import run from django.core.handlers.wsgi import WSGIHandler argparser = argparse.ArgumentParser( prog='mathicsserver', usage='%(prog)s [options]', add_help=False, description="""Mathics server for the graphical user interface in a web browser. It is not intended for production use on a public Web server!""", epilog="""Please feel encouraged to contribute to Mathics! Create your own fork, make the desired changes, commit, and make a pull request.""") argparser.add_argument( '--help', '-h', help='show this help message and exit', action='help') argparser.add_argument( '--quiet', '-q', help='don\'t print message at startup', action='store_true') argparser.add_argument( '--version', '-v', action='version', version='%(prog)s ' + mathics.__version__) argparser.add_argument( "--port", "-p", dest="port", metavar="PORT", default=8000, type=int, help="use PORT as server port") argparser.add_argument( "--external", "-e", dest="external", action="store_true", help="allow external access to server") args = argparser.parse_args() quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C' port = args.port if not args.quiet: print_version(is_server=True) print_license() print u"Quit by pressing %s\n" % quit_command print u"""Open the graphical user interface at http://localhost:%d\nin Firefox, Chrome, or Safari to use Mathics\n""" % port if args.external: addr = '0.0.0.0' else: addr = '127.0.0.1' try: if settings.DJANGO_VERSION < (1, 4): from django.core.servers.basehttp import AdminMediaHandler handler = AdminMediaHandler(WSGIHandler(), '') else: from django.core.servers.basehttp import ( get_internal_wsgi_application) handler = get_internal_wsgi_application() run(addr, port, handler) except socket.error as e: # Use helpful error messages instead of ugly tracebacks. ERRORS = { errno.EACCES: "You don't have permission to access that port.", errno.EADDRINUSE: "That port is already in use.", errno.EADDRNOTAVAIL: "That IP address can't be assigned to.", } try: error_text = ERRORS[e.errno] except KeyError: error_text = str(e) sys.stderr.write("Error: %s" % error_text + '\n') # Need to use an OS exit because sys.exit doesn't work in a thread os._exit(1) except KeyboardInterrupt: print "\nGoodbye!\n" sys.exit(0)
def inner_run(): print("Validating models...") self.validate(display_num_errors=True) print("\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)) print("Development server is running at http://%s:%s/" % (addr, port)) print("Using the Werkzeug debugger (http://werkzeug.pocoo.org/)") print("Quit the server with %s." % quit_command) path = options.get('admin_media_path', '') if not path: admin_media_path = os.path.join(django.__path__[0], 'contrib/admin/static/admin') if os.path.isdir(admin_media_path): path = admin_media_path else: path = os.path.join(django.__path__[0], 'contrib/admin/media') handler = WSGIHandler() if USE_ADMINMEDIAHANDLER: handler = AdminMediaHandler(handler, path) if USE_STATICFILES: use_static_handler = options.get('use_static_handler', True) insecure_serving = options.get('insecure_serving', False) if use_static_handler and (settings.DEBUG or insecure_serving): handler = StaticFilesHandler(handler) if open_browser: import webbrowser url = "http://%s:%s/" % (addr, port) webbrowser.open(url) if cert_path: """ OpenSSL is needed for SSL support. This will make flakes8 throw warning since OpenSSL is not used directly, alas, this is the only way to show meaningful error messages. See: http://lucumr.pocoo.org/2011/9/21/python-import-blackbox/ for more information on python imports. """ try: import OpenSSL # NOQA except ImportError: raise CommandError( "Python OpenSSL Library is " "required to use runserver_plus with ssl support. " "Install via pip (pip install pyOpenSSL).") dir_path, cert_file = os.path.split(cert_path) if not dir_path: dir_path = os.getcwd() root, ext = os.path.splitext(cert_file) certfile = os.path.join(dir_path, root + ".crt") keyfile = os.path.join(dir_path, root + ".key") try: from werkzeug.serving import make_ssl_devcert if os.path.exists(certfile) and \ os.path.exists(keyfile): ssl_context = (certfile, keyfile) else: # Create cert, key files ourselves. ssl_context = make_ssl_devcert(os.path.join( dir_path, root), host='localhost') except ImportError: print( "Werkzeug version is less than 0.9, trying adhoc certificate." ) ssl_context = "adhoc" else: ssl_context = None run_simple(addr, int(port), DebuggedApplication(handler, True), use_reloader=use_reloader, use_debugger=True, threaded=threaded, ssl_context=ssl_context)
def make_wsgi(req): # twisted.web2 can not insert 'PATH_INFO' into environment. os.environ["PATH_INFO"] = req.path os.environ["SCRIPT_URL"] = req.path return wsgi.WSGIResource(AdminMediaHandler(WSGIHandler()))
def setUp(self): self.admin_media_url = urljoin(settings.STATIC_URL, "admin/") self.admin_media_file_path = os.path.abspath( os.path.join(django.__path__[0], "contrib", "admin", "static", "admin") ) self.handler = AdminMediaHandler(WSGIHandler())
def inner_run(): from django.conf import settings import hotshot, time, os USE_CPROFILE = options.get('use_cprofile', False) USE_LSPROF = options.get('use_lsprof', False) if USE_LSPROF: USE_CPROFILE = True if USE_CPROFILE: try: import cProfile USE_CPROFILE = True except ImportError: print "cProfile disabled, module cannot be imported!" USE_CPROFILE = False if USE_LSPROF and not USE_CPROFILE: raise SystemExit("Kcachegrind compatible output format required cProfile from Python 2.5") prof_path = options.get('prof_path', '/tmp') def make_profiler_handler(inner_handler): def handler(environ, start_response): path_info = environ['PATH_INFO'] # normally /media/ is MEDIA_URL, but in case still check it in case it's differently # should be hardly a penalty since it's an OR expression. # TODO: fix this to check the configuration settings and not make assumpsions about where media are on the url if no_media and (path_info.startswith('/media') or path_info.startswith(settings.MEDIA_URL)): return inner_handler(environ, start_response) path_name = path_info.strip("/").replace('/', '.') or "root" profname = "%s.%s.prof" % (path_name, datetime.now().isoformat()) profname = os.path.join(prof_path, profname) if USE_CPROFILE: prof = cProfile.Profile() else: prof = hotshot.Profile(profname) start = datetime.now() try: return prof.runcall(inner_handler, environ, start_response) finally: # seeing how long the request took is important! elap = datetime.now() - start elapms = elap.seconds * 1000.0 + elap.microseconds / 1000.0 if USE_LSPROF: kg = KCacheGrind(prof) kg.output(file(profname, 'w')) elif USE_CPROFILE: prof.dump_stats(profname) profname2 = "%s.%06dms.%s.prof" % (path_name, elapms, datetime.now().isoformat()) profname2 = os.path.join(prof_path, profname2) os.rename(profname, profname2) return handler print "Validating models..." self.validate(display_num_errors=True) print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE) print "Development server is running at http://%s:%s/" % (addr, port) print "Quit the server with %s." % quit_command try: path = admin_media_path or django.__path__[0] + '/contrib/admin/media' handler = make_profiler_handler(AdminMediaHandler(WSGIHandler(), path)) run(addr, int(port), handler) except WSGIServerException, e: # Use helpful error messages instead of ugly tracebacks. ERRORS = { 13: "You don't have permission to access that port.", 98: "That port is already in use.", 99: "That IP address can't be assigned-to.", } try: error_text = ERRORS[e.args[0].args[0]] except (AttributeError, KeyError): error_text = str(e) sys.stderr.write(self.style.ERROR("Error: %s" % error_text) + '\n') # Need to use an OS exit because sys.exit doesn't work in a thread os._exit(1)
def setUp(self): app = AdminMediaHandler(WSGIHandler()) twill.add_wsgi_intercept(self.HOST, self.PORT, lambda: app) twill.set_output(StringIO()) self.command = twill.commands
def inner_run(): from django.conf import settings from django.utils import translation print "Validating models..." self.validate(display_num_errors=True) print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE) print "Development server is running at http://%s:%s/" % (addr, port) print "Quit the server with %s." % quit_command # django.core.management.base forces the locale to en-us. We should # set it up correctly for the first request (particularly important # in the "--noreload" case). translation.activate(settings.LANGUAGE_CODE) try: # BEGIN profiling import cProfile import pstats import time, tempfile profile_temp_dir = None if profile_temp_dir is not None: tempfile.tempdir = profile_temp_dir def make_profiler_handler(inner_handler): def handler(environ, start_response): print "============= Starting response ====================" path = environ['PATH_INFO'] if path.startswith(settings.MEDIA_URL): return inner_handler(environ, start_response) path = path.strip('/').replace('/', '.') if path: prefix = 'p.%s.%3f' % (path, time.time()) else: prefix = 'p.%3f' % time.time() fd, profname = tempfile.mkstemp('.prof', prefix) os.close(fd) prof = cProfile.Profile() try: return prof.runcall(inner_handler, environ, start_response) finally: prof.dump_stats(profname) stats = pstats.Stats(profname) stats.sort_stats('cumulative', 'time', 'calls') stats.print_stats('lingcod',20) print " * Complete cProfile output at %s" % profname print "============= Done =================================" return handler # END #handler = AdminMediaHandler(WSGIHandler(), admin_media_path) handler = make_profiler_handler(AdminMediaHandler(WSGIHandler(), admin_media_path)) run(addr, int(port), handler) except WSGIServerException, e: # Use helpful error messages instead of ugly tracebacks. ERRORS = { 13: "You don't have permission to access that port.", 98: "That port is already in use.", 99: "That IP address can't be assigned-to.", } try: error_text = ERRORS[e.args[0].args[0]] except (AttributeError, KeyError): error_text = str(e) sys.stderr.write(self.style.ERROR("Error: %s" % error_text) + '\n') # Need to use an OS exit because sys.exit doesn't work in a thread os._exit(1)
def run(self): self.lock.acquire() pidfile = os.path.join(tempfile.gettempdir(), 'lettuce-django.pid') if os.path.exists(pidfile): pid = int(open(pidfile).read()) try: os.kill(pid, 9) except OSError: pass finally: os.unlink(pidfile) open(pidfile, 'w').write(unicode(os.getpid())) self.configure_mail_queue() connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s = connector.connect((self.address, self.port)) self.lock.release() os.kill(os.getpid(), 9) except socket.error: pass finally: self.lock.release() try: server_address = (self.address, self.port) httpd = WSGIServer(server_address, MutedRequestHandler) except WSGIServerException: raise LettuceServerException( "the port %d already being used, could not start " \ "django's builtin server on it" % self.port, ) handler = WSGIHandler() if self.should_serve_admin_media(): if not AdminMediaHandler: raise LettuceServerException( "AdminMediaHandler is not available in this version of " "Django. Please set LETTUCE_SERVE_ADMIN_MEDIA = False " "in your Django settings.") admin_media_path = '' handler = AdminMediaHandler(handler, admin_media_path) if self.should_serve_static_files(): handler = StaticFilesHandler(handler) httpd.set_app(handler) global keep_running while keep_running: call_hook('before', 'handle_request', httpd, self) httpd.handle_request() call_hook('after', 'handle_request', httpd, self) try: self.lock.release() except ValueError: pass
def get_handler(self, *args, **options): """ Serves admin media like old-school (deprecation pending). """ handler = super(Command, self).get_handler(*args, **options) return AdminMediaHandler(handler, options.get('admin_media_path', ''))
def inner_run(): import os import time import hotshot USE_CPROFILE = options.get('use_cprofile', False) USE_LSPROF = options.get('use_lsprof', False) if USE_LSPROF: USE_CPROFILE = True if USE_CPROFILE: try: import cProfile USE_CPROFILE = True except ImportError: print "cProfile disabled, module cannot be imported!" USE_CPROFILE = False if USE_LSPROF and not USE_CPROFILE: raise SystemExit("Kcachegrind compatible output format required cProfile from Python 2.5") prof_path = options.get('prof_path', '/tmp') def get_exclude_paths(): exclude_paths = [] media_url = getattr(settings, 'MEDIA_URL', None) if media_url: exclude_paths.append(media_url) static_url = getattr(settings, 'STATIC_URL', None) if static_url: exclude_paths.append(static_url) admin_media_prefix = getattr(settings, 'ADMIN_MEDIA_PREFIX', None) if admin_media_prefix: exclude_paths.append(admin_media_prefix) return exclude_paths def make_profiler_handler(inner_handler): def handler(environ, start_response): path_info = environ['PATH_INFO'] # when using something like a dynamic site middleware is could be necessary # to refetch the exclude_paths every time since they could change per site. if no_media and any(path_info.startswith(p) for p in get_exclude_paths()): return inner_handler(environ, start_response) path_name = path_info.strip("/").replace('/', '.') or "root" profname = "%s.%d.prof" % (path_name, time.time()) profname = os.path.join(prof_path, profname) if USE_CPROFILE: prof = cProfile.Profile() else: prof = hotshot.Profile(profname) start = datetime.now() try: return prof.runcall(inner_handler, environ, start_response) finally: # seeing how long the request took is important! elap = datetime.now() - start elapms = elap.seconds * 1000.0 + elap.microseconds / 1000.0 if USE_LSPROF: kg = KCacheGrind(prof) kg.output(file(profname, 'w')) elif USE_CPROFILE: prof.dump_stats(profname) profname2 = "%s.%06dms.%d.prof" % (path_name, elapms, time.time()) profname2 = os.path.join(prof_path, profname2) if not USE_CPROFILE: prof.close() os.rename(profname, profname2) return handler print "Validating models..." self.validate(display_num_errors=True) print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE) print "Development server is running at http://%s:%s/" % (addr, port) print "Quit the server with %s." % quit_command path = options.get('admin_media_path', '') if not path: admin_media_path = os.path.join(django.__path__[0], 'contrib/admin/static/admin') if os.path.isdir(admin_media_path): path = admin_media_path else: path = os.path.join(django.__path__[0], 'contrib/admin/media') try: handler = AdminMediaHandler(WSGIHandler(), path) if USE_STATICFILES: use_static_handler = options.get('use_static_handler', True) insecure_serving = options.get('insecure_serving', False) if (use_static_handler and (settings.DEBUG or insecure_serving)): handler = StaticFilesHandler(handler) handler = make_profiler_handler(handler) run(addr, int(port), handler) except WSGIServerException, e: # Use helpful error messages instead of ugly tracebacks. ERRORS = { 13: "You don't have permission to access that port.", 98: "That port is already in use.", 99: "That IP address can't be assigned-to.", } try: error_text = ERRORS[e.args[0].args[0]] except (AttributeError, KeyError): error_text = str(e) sys.stderr.write(self.style.ERROR("Error: %s" % error_text) + '\n') # Need to use an OS exit because sys.exit doesn't work in a thread os._exit(1)
def start(): #NOTE bots is always on PYTHONPATH!!! - otherwise it will not start. #********command line arguments************************** configdir = 'config' for arg in sys.argv[1:]: if not arg: continue if arg.startswith('-c'): configdir = arg[2:] if not configdir: print 'Configuration directory indicated, but no directory name.' sys.exit(1) elif arg in ["?", "/?"] or arg.startswith('-'): showusage() else: showusage() #init general: find locating of bots, configfiles, init paths etc.*********************** botsinit.generalinit(configdir) #init cherrypy; only needed for webserver. ********************************************* cherrypy.config.update({'global': { 'tools.staticdir.root': botsglobal.ini.get('directories','botspath'), 'server.socket_host' : "0.0.0.0", #to what IP addresses should be server. 0.0.0.0: all. See cherrypy docs 'server.socket_port': botsglobal.ini.getint('webserver','port',8080), 'server.environment': botsglobal.ini.get('webserver','environment','development'), # development production 'log.screen': False, #~ 'log.error_file': '', #set later to rotating log file #~ 'log.access_file': '', #set later to rotating log file }}) conf = {'/': {'tools.staticdir.on' : True,'tools.staticdir.dir' : 'media' }} #~ '/favicon.ico': {'tools.staticfile.on': True,'tools.staticfile.filename': '/home/hje/botsup-django/bots/media/images/favicon.ico'}} cherrypy.tree.graft(AdminMediaHandler(WSGIHandler()), '/') myroot = Root() myappl = cherrypy.tree.mount(myroot, '/media', conf) #myappl is needed to set logging botsglobal.logger = logging.getLogger('webserver') botsglobal.logger.setLevel(logging.DEBUG) h = logging.handlers.TimedRotatingFileHandler(botslib.join(botsglobal.ini.get('directories','logging'),'webserver.log'),when='midnight', backupCount=10) fileformat = logging.Formatter("%(asctime)s %(levelname)-8s %(name)s : %(message)s",'%Y%m%d %H:%M:%S') h.setFormatter(fileformat) # add rotating file handler to main logger botsglobal.logger.addHandler(h) # Make RotatingFileHandler for the error log. #~ h = logging.handlers.TimedRotatingFileHandler(botslib.join(botsglobal.ini.get('directories','logging'),'webserver.log'),when='midnight', backupCount=10) #~ fileformat = logging.Formatter("%(asctime)s %(levelname)-8s %(name)s : %(message)s",'%Y%m%d %H:%M:%S') #~ h.setLevel(logging.INFO) #~ h.setFormatter(fileformat) #~ myappl.log.error_log.addHandler(h) # MakeRotatingFileHandler for the access log. #~ h = logging.handlers.TimedRotatingFileHandler(os.path.normpath(os.path.join(botsglobal.ini.get('directories','botspath'), 'botssys/logging/webserver.log')),when='midnight', backupCount=10) #~ h.setLevel(logging.DEBUG) #~ myappl.log.access_log.addHandler(h) #~ botsglobal.logger = myappl.log.access_log #write start info to cherrypy log******************************************** botsglobal.logger.info(_(u'Bots web server started.')) botsglobal.logger.info(_(u'Python version: "%s".'),sys.version) botsglobal.logger.info(_(u'Django version: "%s".'),django.VERSION) #start cherrypy ********************************************************************* cherrypy.engine.start() cherrypy.engine.block()
def setUp(self): self.admin_media_url = urljoin(settings.STATIC_URL, 'admin/') self.admin_media_file_path = os.path.abspath( os.path.join(django.__path__[0], 'contrib', 'admin', 'static', 'admin')) self.handler = AdminMediaHandler(WSGIHandler())
def inner_run(): import os import time try: import hotshot except ImportError: pass # python 3.x USE_CPROFILE = options.get('use_cprofile', False) USE_LSPROF = options.get('use_lsprof', False) if USE_LSPROF: USE_CPROFILE = True if USE_CPROFILE: try: import cProfile USE_CPROFILE = True except ImportError: print("cProfile disabled, module cannot be imported!") USE_CPROFILE = False if USE_LSPROF and not USE_CPROFILE: raise SystemExit( "Kcachegrind compatible output format required cProfile from Python 2.5" ) prof_path = options.get('prof_path', '/tmp') prof_file = options.get('prof_file', '{path}.{duration:06d}ms.{time}') if not prof_file.format(path='1', duration=2, time=3): prof_file = '{path}.{duration:06d}ms.{time}' print( "Filename format is wrong. Default format used: '{path}.{duration:06d}ms.{time}'." ) def get_exclude_paths(): exclude_paths = [] media_url = getattr(settings, 'MEDIA_URL', None) if media_url: exclude_paths.append(media_url) static_url = getattr(settings, 'STATIC_URL', None) if static_url: exclude_paths.append(static_url) admin_media_prefix = getattr(settings, 'ADMIN_MEDIA_PREFIX', None) if admin_media_prefix: exclude_paths.append(admin_media_prefix) return exclude_paths def make_profiler_handler(inner_handler): def handler(environ, start_response): path_info = environ['PATH_INFO'] # when using something like a dynamic site middleware is could be necessary # to refetch the exclude_paths every time since they could change per site. if no_media and any( path_info.startswith(p) for p in get_exclude_paths()): return inner_handler(environ, start_response) path_name = path_info.strip("/").replace('/', '.') or "root" profname = "%s.%d.prof" % (path_name, time.time()) profname = os.path.join(prof_path, profname) if USE_CPROFILE: prof = cProfile.Profile() else: prof = hotshot.Profile(profname) start = datetime.now() try: return prof.runcall(inner_handler, environ, start_response) finally: # seeing how long the request took is important! elap = datetime.now() - start elapms = elap.seconds * 1000.0 + elap.microseconds / 1000.0 if USE_LSPROF: kg = KCacheGrind(prof) kg.output(open(profname, 'w')) elif USE_CPROFILE: prof.dump_stats(profname) profname2 = prof_file.format(path=path_name, duration=int(elapms), time=int(time.time())) profname2 = os.path.join(prof_path, "%s.prof" % profname2) if not USE_CPROFILE: prof.close() os.rename(profname, profname2) return handler print("Validating models...") if hasattr(self, 'check'): self.check(display_num_errors=True) else: self.validate(display_num_errors=True) print("\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)) print("Development server is running at http://%s:%s/" % (addr, port)) print("Quit the server with %s." % quit_command) path = options.get('admin_media_path', '') if not path: admin_media_path = os.path.join(django.__path__[0], 'contrib/admin/static/admin') if os.path.isdir(admin_media_path): path = admin_media_path else: path = os.path.join(django.__path__[0], 'contrib/admin/media') try: handler = WSGIHandler() if HAS_ADMINMEDIAHANDLER: handler = AdminMediaHandler(handler, path) if USE_STATICFILES: use_static_handler = options.get('use_static_handler', True) insecure_serving = options.get('insecure_serving', False) if (use_static_handler and (settings.DEBUG or insecure_serving)): handler = StaticFilesHandler(handler) handler = make_profiler_handler(handler) run(addr, int(port), handler) except wsgi_server_exc_cls as e: # Use helpful error messages instead of ugly tracebacks. ERRORS = { errno.EACCES: "You don't have permission to access that port.", errno.EADDRINUSE: "That port is already in use.", errno.EADDRNOTAVAIL: "That IP address can't be assigned-to.", } if not isinstance(e, socket.error): # Django < 1.6 ERRORS[13] = ERRORS.pop(errno.EACCES) ERRORS[98] = ERRORS.pop(errno.EADDRINUSE) ERRORS[99] = ERRORS.pop(errno.EADDRNOTAVAIL) try: if not isinstance(e, socket.error): # Django < 1.6 error_text = ERRORS[e.args[0].args[0]] else: error_text = ERRORS[e.errno] except (AttributeError, KeyError): error_text = str(e) sys.stderr.write( self.style.ERROR("Error: %s" % error_text) + '\n') # Need to use an OS exit because sys.exit doesn't work in a thread os._exit(1) except KeyboardInterrupt: if shutdown_message: print(shutdown_message) sys.exit(0)
def inner_run(): # Flag the server as active from devserver import settings import devserver settings.DEVSERVER_ACTIVE = True settings.DEBUG = True from django.conf import settings from django.utils import translation print "Validating models..." self.validate(display_num_errors=True) print "\nDjango version %s, using settings %r" % ( django.get_version(), settings.SETTINGS_MODULE) print "Running django-devserver %s" % (devserver.get_version(), ) if use_werkzeug: server_type = 'werkzeug' else: server_type = 'django' print "%s %s server is running at http://%s:%s/" % ( options['use_forked'] and 'Forked' or 'Threaded', server_type, addr, port) print "Quit the server with %s." % quit_command # django.core.management.base forces the locale to en-us. We should # set it up correctly for the first request (particularly important # in the "--noreload" case). translation.activate(settings.LANGUAGE_CODE) if int(options['verbosity']) < 1: app = WSGIHandler() else: app = DevServerHandler() if wsgi_app: try: app = __import__(wsgi_app, {}, {}, ['application']).application except (ImportError, AttributeError): raise if options['use_forked']: mixin = SocketServer.ForkingMixIn else: mixin = SocketServer.ThreadingMixIn middleware = getattr(settings, 'DEVSERVER_WSGI_MIDDLEWARE', []) for middleware in middleware: module, class_name = middleware.rsplit('.', 1) app = getattr(__import__(module, {}, {}, [class_name]), class_name)(app) if 'django.contrib.staticfiles' in settings.INSTALLED_APPS and use_static_files: from django.contrib.staticfiles.handlers import StaticFilesHandler app = StaticFilesHandler(app) else: app = AdminMediaHandler(app, admin_media_path) if options['use_dozer']: from dozer import Dozer app = Dozer(app) try: if use_werkzeug: run_simple(addr, int(port), DebuggedApplication(app, True), use_reloader=False, use_debugger=True) else: run(addr, int(port), app, mixin) except WSGIServerException, e: # Use helpful error messages instead of ugly tracebacks. ERRORS = { 13: "You don't have permission to access that port.", 98: "That port is already in use.", 99: "That IP address can't be assigned-to.", } try: error_text = ERRORS[e.args[0].args[0]] except (AttributeError, KeyError): error_text = str(e) sys.stderr.write( self.style.ERROR("Error: %s" % error_text) + '\n') # Need to use an OS exit because sys.exit doesn't work in a thread os._exit(1)
def make_command_wsgi_application(admin_mediapath): reload_django_settings() return AdminMediaHandler(make_wsgi_application(), admin_mediapath)
def setUp(self): self.admin_media_url = urljoin(settings.STATIC_URL, 'admin/') self.admin_media_file_path = os.path.abspath( os.path.join(django.__path__[0], 'contrib', 'admin', 'static', 'admin') ) self.handler = AdminMediaHandler(WSGIHandler())
def setUp(self): self.admin_media_file_path = os.path.abspath( os.path.join(django.__path__[0], 'contrib', 'admin', 'media')) self.handler = AdminMediaHandler(WSGIHandler())
def setUp(self): self.admin_media_file_path = \ os.path.join(django.__path__[0], 'contrib', 'admin', 'media') self.handler = AdminMediaHandler(WSGIHandler())
import os from wsgiref.simple_server import make_server from django.core.handlers.wsgi import WSGIHandler from django.core.servers.basehttp import AdminMediaHandler os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' httpd = make_server('', 8000, AdminMediaHandler(WSGIHandler())) httpd.serve_forever()