def start_server(options): """ Start CherryPy server """ if options['daemonize'] and options['server_user'] and options['server_group']: # ensure the that the daemon runs as specified user change_uid_gid(options['server_user'], options['server_group']) try: from cheroot.wsgi import Server as Server except ImportError: from cherrypy.wsgiserver import CherryPyWSGIServer as Server from django.core.handlers.wsgi import WSGIHandler server = Server( (options['host'], int(options['port'])), WSGIHandler(), int(options['threads']), options['server_name'] ) if options['ssl_certificate'] and options['ssl_private_key']: server.ssl_certificate = options['ssl_certificate'] server.ssl_private_key = options['ssl_private_key'] try: server.start() except KeyboardInterrupt: server.stop()
def run(self, handler): # pragma: no cover from cheroot.wsgi import Server as WSGIServer self.options['bind_addr'] = (self.host, self.port) self.options['wsgi_app'] = handler certfile = self.options.get('certfile') if certfile: del self.options['certfile'] keyfile = self.options.get('keyfile') if keyfile: del self.options['keyfile'] server = WSGIServer(**self.options) if certfile: server.ssl_certificate = certfile if keyfile: server.ssl_private_key = keyfile try: server.start() finally: server.stop()
def start(): #NOTE: bots directory should always be on PYTHONPATH - otherwise it will not start. #***command line arguments************************** usage = ''' This is "%(name)s" version %(version)s, part of Bots open source edi translator (http://bots.sourceforge.net). The %(name)s is the web server for bots; the interface (bots-monitor) can be accessed in a browser, eg 'http://localhost:8080'. Usage: %(name)s -c<directory> Options: -c<directory> directory for configuration files (default: config). ''' % { 'name': os.path.basename(sys.argv[0]), 'version': botsglobal.version } configdir = 'config' for arg in sys.argv[1:]: if arg.startswith('-c'): configdir = arg[2:] if not configdir: print( 'Error: configuration directory indicated, but no directory name.' ) sys.exit(1) else: print(usage) sys.exit(0) #***end handling command line arguments************************** botsinit.generalinit( configdir) #find locating of bots, configfiles, init paths etc. process_name = 'webserver' botsglobal.logger = botsinit.initserverlogging( process_name ) #initialise file-logging for web-server. This logging only contains the logging from bots-webserver, not from cherrypy. #***init cherrypy as webserver********************************************* #global configuration for cherrypy cherrypy.config.update({ 'global': { 'log.screen': False, 'server.environment': botsglobal.ini.get('webserver', 'environment', 'production') } }) #cherrypy handling of static files conf = { '/': { 'tools.staticdir.on': True, 'tools.staticdir.dir': 'media', 'tools.staticdir.root': botsglobal.ini.get('directories', 'botspath') } } servestaticfiles = cherrypy.tree.mount( None, '/media', conf ) #None: no cherrypy application (as this only serves static files) #cherrypy handling of django servedjango = WSGIHandler( ) #was: servedjango = AdminMediaHandler(WSGIHandler()) - django does not need the AdminMediaHandler. #cherrypy uses a dispatcher in order to handle the serving of static files and django. dispatcher = WSGIPathInfoDispatcher({ '/': servedjango, str('/media'): servestaticfiles }) #UNICODEPROBLEM: needs to be binary botswebserver = WSGIServer( bind_addr=('0.0.0.0', botsglobal.ini.getint('webserver', 'port', 8080)), wsgi_app=dispatcher, server_name=botsglobal.ini.get('webserver', 'name', 'bots-webserver')) #botswebserver = wsgiserver.CherryPyWSGIServer(bind_addr=('0.0.0.0', botsglobal.ini.getint('webserver','port',8080)), wsgi_app=dispatcher, server_name=botsglobal.ini.get('webserver','name','bots-webserver')) botsglobal.logger.log(25, _('Bots %(process_name)s started.'), {'process_name': process_name}) botsglobal.logger.log( 25, _('Bots %(process_name)s configdir: "%(configdir)s".'), { 'process_name': process_name, 'configdir': botsglobal.ini.get('directories', 'config') }) botsglobal.logger.log( 25, _('Bots %(process_name)s serving at port: "%(port)s".'), { 'process_name': process_name, 'port': botsglobal.ini.getint('webserver', 'port', 8080) }) #handle ssl: cherrypy < 3.2 always uses pyOpenssl. cherrypy >= 3.2 uses python buildin ssl (python >= 2.6 has buildin support for ssl). ssl_certificate = botsglobal.ini.get('webserver', 'ssl_certificate', None) ssl_private_key = botsglobal.ini.get('webserver', 'ssl_private_key', None) if ssl_certificate and ssl_private_key: if StrictVersion(cherrypy.__version__) >= StrictVersion('3.2.0'): botswebserver.ssl_adapter = BuiltinSSLAdapter( ssl_certificate, ssl_private_key) else: #but: pyOpenssl should be there! botswebserver.ssl_certificate = ssl_certificate botswebserver.ssl_private_key = ssl_private_key botsglobal.logger.log(25, _('Bots %(process_name)s uses ssl (https).'), {'process_name': process_name}) else: botsglobal.logger.log( 25, _('Bots %(process_name)s uses plain http (no ssl).'), {'process_name': process_name}) #***start the cherrypy webserver.************************************************ try: botswebserver.start() except KeyboardInterrupt: botswebserver.stop()