def server_function(self): self.dispatcher = wsgiserver.WSGIPathInfoDispatcher({'/': self.app}) self.server = wsgiserver.WSGIServer(self.dispatcher, host=self.host, port=self.port) try: self.server.start() except KeyboardInterrupt: self.logger("Stopping server due to keyboard interrupt") self.server.stop()
def initialize(): """Init function for this module""" with INIT_LOCK: global __INITIALIZED__, app, FULL_PATH, RUNDIR, ARGS, DAEMON, PIDFILE, VERBOSE, LOG_FILE, LOG_DIR, logger, PORT, SERVER, DATABASE, AUTH, \ UPDATER, CURRENT_COMMIT, LATEST_COMMIT, COMMITS_BEHIND, COMMITS_COMPARE_URL, USE_GIT, WEBROOT, HOST, KIOSK, DATA_DIR, SCRIPT_DIR, \ THREADS, FIRST_RUN if __INITIALIZED__: return False # Set up logger if not LOG_FILE: LOG_FILE = os.path.join(DATA_DIR, 'logs', 'maraschino.log') FILENAME = os.path.basename(LOG_FILE) LOG_DIR = LOG_FILE[:-len(FILENAME)] if not os.path.exists(LOG_DIR): try: os.makedirs(LOG_DIR) except OSError: if VERBOSE: print 'Unable to create the log directory.' logger = maraschinoLogger(LOG_FILE, VERBOSE) #set up script dir if not SCRIPT_DIR: SCRIPT_DIR = os.path.join(DATA_DIR, 'scripts') if not os.path.exists(SCRIPT_DIR): os.makedirs(SCRIPT_DIR) # check if database exists or create it from database import init_db if KIOSK: logger.log('Running in KIOSK Mode, settings disabled.', 'INFO') try: logger.log('Opening database at: %s' % (DATABASE), 'INFO') open(DATABASE) except IOError: logger.log('Opening database failed', 'WARNING') #Check if a version file exists. If not assume latest revision. version_file = os.path.join(DATA_DIR, 'Version.txt') if not os.path.exists(version_file): FIRST_RUN = 1 try: logger.log('Checking if PATH exists: %s' % (DATABASE), 'WARNING') dbpath = os.path.dirname(DATABASE) if not os.path.exists(dbpath): try: logger.log('It does not exist, creating it...', 'WARNING') os.makedirs(dbpath) except: logger.log('Could not create %s.' % (DATABASE), 'CRITICAL') print 'Could not create %s.' % (DATABASE) quit() except: logger.log('Could not create %s.' % (DATABASE), 'CRITICAL') quit() logger.log('Database successfully initialised', 'INFO') init_db() # Web server settings from tools import get_setting_value if get_setting_value('maraschino_port'): port_arg = False for arg in ARGS: if arg == '--port' or arg == '-p': port_arg = True if not port_arg: PORT = int(get_setting_value('maraschino_port')) # Set up AUTH username = get_setting_value('maraschino_username') password = get_setting_value('maraschino_password') if username and password != None: AUTH = {'username': username, 'password': password} # Set up web server if '--webroot' not in str(ARGS): WEBROOT = get_setting_value('maraschino_webroot') if WEBROOT is None or DEVELOPMENT: WEBROOT = '' if WEBROOT: if WEBROOT[0] != '/': WEBROOT = '/' + WEBROOT d = wsgiserver.WSGIPathInfoDispatcher({WEBROOT: app}) else: d = wsgiserver.WSGIPathInfoDispatcher({'/': app}) SERVER = wsgiserver.CherryPyWSGIServer((HOST, PORT), d) __INITIALIZED__ = True return True
start_response(status, headers) except Exception as e: raise finally: appd.end_bt(custom_bt, e) return [body_str_byte] def my_orders(environ, start_response): custom_bt = appd.start_bt('My orders BT') body_str = 'My orders\n' body_str_byte = bytes(body_str, 'utf-8') status = '200 OK' headers = [('Content-type', 'text/text')] e = None try: start_response(status, headers) except Exception as e: raise finally: appd.end_bt(custom_bt, e) return [body_str_byte] d = wsgiserver.WSGIPathInfoDispatcher({'/': application, '/orders': my_orders}) server = wsgiserver.WSGIServer(d) server.start()
return [str(rtt)] def get_rtt(ip, port): #TODO get rtt key = ip+port cond.acquire() while key not in ips: cond.wait() #sleep while "rtt" not in ips[key]: cond.wait() rtt = ips[key]["rtt"] ##check if ip exists and status cond.release() return rtt, None err = "Error" return None, err d = wsgiserver.WSGIPathInfoDispatcher({'/rtt': my_rtt}) server = wsgiserver.WSGIServer(d, host=host, port=port) #To add SSL support, just specify a certfile and a keyfile #server = wsgiserver.WSGIServer(my_app, certfile='cert.pem', keyfile='privkey.pem') print("Server running on: ", host, port) server.start() print("server stopped")
try: frozen = sys.frozen except AttributeError: frozen = False # Define path based on frozen state if frozen: path_base = os.environ['_MEIPASS2'] rundir = os.path.dirname(sys.executable) else: path_base = rundir # Include paths sys.path.insert(0, path_base) sys.path.insert(0, os.path.join(path_base, 'lib')) import wsgiserver from Maraschino import app from settings import * d = wsgiserver.WSGIPathInfoDispatcher({'/': app}) server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', CHERRYPY_PORT), d) if __name__ == '__main__': try: server.start() except KeyboardInterrupt: server.stop()