示例#1
0
    def run_server(self, port=None):

        log.info("home_dir %s",config.get('DEFAULT', 'home_dir'))
        log.info("hydra_base_dir %s",config.get('DEFAULT', 'hydra_base_dir'))
        log.info("common_app_data_folder %s",config.get('DEFAULT', 'common_app_data_folder'))
        log.info("win_common_documents %s",config.get('DEFAULT', 'win_common_documents'))
        log.info("sqlite url %s",config.get('mysqld', 'url'))
        log.info("layout_xsd_path %s",config.get('hydra_server', 'layout_xsd_path'))
        log.info("default_directory %s",config.get('plugin', 'default_directory'))
        log.info("result_file %s",config.get('plugin', 'result_file'))
        log.info("plugin_xsd_path %s",config.get('plugin', 'plugin_xsd_path'))
        log.info("log_config_path %s",config.get('logging_conf', 'log_config_path'))

        if port is None:
            port = config.getint('hydra_server', 'port', 8080)

        domain = config.get('hydra_server', 'domain', '127.0.0.1')

        check_port_available(domain, port)

        spyne.const.xml_ns.DEFAULT_NS = 'soap_server.hydra_complexmodels'
        cp_wsgi_application = Server((domain,port), application, numthreads=10)

        log.info("listening to http://%s:%s", domain, port)
        log.info("wsdl is at: http://%s:%s/soap/?wsdl", domain, port)

        try:
            cp_wsgi_application.start()
        except KeyboardInterrupt:
            cp_wsgi_application.stop()
示例#2
0
def launch_server(host, port, app, **kwargs):
    """Use cheroot WSGI server, a multithreaded scallable server."""

    server = Server((host, port), app, **kwargs)
    logging.info("Starting server, listening on port %s", port)
    try:
        server.start()
    except KeyboardInterrupt:
        server.stop()
示例#3
0
class Controller (object):
    def listen(self):
        print("listening")
        self.server = WSGIServer(('0.0.0.0', 8000), webapp)
        # run(webapp, server=self.server)
        # run(webapp, server='bjoern', port=8000)
        self.server.start()

    def shutdown(self):
        self.server.stop()
        print("done listening")
def start_server(config):
    from cheroot.wsgi import Server as WSGIServer
    hostname = config['server_host']['host']
    port = int(config['server_host']['port'])
    scheme = config['server_host']['scheme']
    app = load_app()
    server = WSGIServer((hostname, port), app)
    try:
        LOGGER.debug('starting Cheroot at %s://%s:%s',
                scheme, hostname, port)
        std_error_message("Starting Cheroot at %s://%s:%s"
                % (scheme, hostname, port))
        server.start()
    except KeyboardInterrupt:
        server.stop()
        sys.exit(0)
示例#5
0
    def run(self):
        logger.info(
            f"HTTP server listens on http://{self.host}:{self.port}{self.endpoint}"
        )
        if self.agent:
            self.agent.server_status.starttime = datetime.now()

        if self.debug:
            self.app.run(host=self.host, port=self.port, debug=self.debug)
        else:
            wsgi_server = WSGIServer(bind_addr=("0.0.0.0", self.wsgi_port),
                                     wsgi_app=self.app,
                                     numthreads=100)
            try:
                wsgi_server.start()
            except KeyboardInterrupt:
                wsgi_server.stop()
示例#6
0
    def test_server(self):
        """Test http server"""
        test_dir, application = self.create_server()

        # Prepare server thread
        server = Server(("localhost", 0), application.rest)
        server.prepare()
        server_port = server.bind_addr[1]
        thread = threading.Thread(target=server.serve)
        thread.start()

        # Run test
        response = urlopen(f"http://localhost:{server_port}/en/cs/unit/Hello/")
        payload = json.loads(response.read().decode("utf-8"))
        assert payload[0]["target"] == "Ahoj"

        # Shutdown the server thread
        server.stop()
        thread.join()
        self.cleanup(test_dir, application)
示例#7
0
    def run(self, handler):
        """
        Run server
        """
        try:
            from cheroot.wsgi import Server as WSGIServer
        except ImportError:
            from cherrypy.wsgiserver import CherryPyWSGIServer as WSGIServer

        global server
        # server = wsgiserver.CherryPyWSGIServer((self.host, self.port), handler)
        server = WSGIServer((self.host, self.port), handler)
        #        cert = 'server.pem' # certificate path
        #        server.ssl_certificate = cert
        #        server.ssl_private_key = cert

        try:
            server.start()
        finally:
            server.stop()
示例#8
0
    def run(self, handler):
        """
        Runs a CherryPy Server using the SSL certificate.
        """
        from cheroot.wsgi import Server as CherryPyWSGIServer
        from cheroot.ssl.builtin import BuiltinSSLAdapter

        server = CherryPyWSGIServer((self.host, self.port), handler)

        server.ssl_adapter = BuiltinSSLAdapter(
            certificate=get_ssl_certificate(),
            private_key=get_ssl_private_key(),
            certificate_chain=get_ssl_certificate_chain()
            if get_ssl_certificate_chain() != "" else None)

        try:
            server.start()
        except Exception as ex:
            LOG.error('Unable to start SSL server: %s' % ex)
            server.stop()
示例#9
0
    def test_server(self):
        """Test http server"""
        test_dir, application = self.create_server()

        # Prepare server thread
        server = Server(('localhost', 0), application.rest)
        server.prepare()
        server_port = server.bind_addr[1]
        thread = threading.Thread(target=server.serve)
        thread.start()

        # Run test
        response = urlopen('http://localhost:{}/en/cs/unit/Hello/'.format(server_port))
        payload = json.loads(response.read().decode('utf-8'))
        assert payload[0]['target'] == 'Ahoj'

        # Shutdown the server thread
        server.stop()
        thread.join()
        self.cleanup(test_dir, application)
示例#10
0
class HTTPServer:
    def __init__(self, port=8090):
        self.mappings = {}
        self.port = port
        self.server = CherryPyWSGIServer(("0.0.0.0", port),
                                         self,
                                         server_name="localhost")
        self.started = False
        self.t = threading.Thread(target=self._start)
        self.t.start()
        time.sleep(0.1)

    def _start(self):
        try:
            self.server.start()
        except Exception as e:
            print('ERROR: failed to start server', str(e))

    def stop(self):
        self.server.stop()
        self.t.join()

    def request(self, path, method='GET', query=None):
        query = query or {}
        response = Respose()

        if isinstance(query, dict):
            query = urllib.parse.urlencode(query)

        self.mappings[path, method, query] = response
        return response

    def __call__(self, environ, start_response):
        _method = environ.get('REQUEST_METHOD', 'GET')
        _path = environ.get('PATH_INFO')

        for (path, method, query_string), response in self.mappings.items():
            if _path == path and _method == method:
                return response(start_response)

        return Respose()(start_response)
示例#11
0
    def test_server(self):
        """Test http server"""
        test_dir, application = self.create_server()

        # Prepare server thread
        server = Server(('localhost', 0), application.rest)
        server.prepare()
        server_port = server.bind_addr[1]
        thread = threading.Thread(target=server.serve)
        thread.start()

        # Run test
        response = urlopen(
            'http://localhost:{}/en/cs/unit/Hello/'.format(server_port))
        payload = json.loads(response.read().decode('utf-8'))
        assert payload[0]['target'] == 'Ahoj'

        # Shutdown the server thread
        server.stop()
        thread.join()
        self.cleanup(test_dir, application)
示例#12
0
    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()
示例#13
0
class WebServer(Thread):
    def __init__(
        self,
        port=defaults.WEB_SERVER_PORT,
        static_path=TESTPLAN_UI_STATIC_DIR,
        data_path="./",
        report_name=TESTPLAN_REPORT,
    ):
        super(WebServer, self).__init__()
        self.host = defaults.WEB_SERVER_HOSTNAME
        self.port = port
        self.static_path = static_path
        self.data_path = data_path
        self.report_name = report_name
        self.server = None

    def _configure_flask_app(self):
        app.config["STATIC_PATH"] = self.static_path
        app.config["DATA_PATH"] = self.data_path
        app.config["TESTPLAN_REPORT_NAME"] = self.report_name
        app.static_folder = os.path.abspath(
            os.path.join(
                app.config["STATIC_PATH"], "testing", "build", "static"
            )
        )

    def run(self):
        self._configure_flask_app()
        dispatcher = PathInfoDispatcher({"/": app})
        self.server = WSGIServer((self.host, self.port), dispatcher)
        self.server.start()

    def ready(self):
        if self.server:
            return self.server.ready
        return False

    def stop(self):
        self.server.stop()
示例#14
0
    def run_server(self, port=None):

        log.info("home_dir %s", config.get('DEFAULT', 'home_dir'))
        log.info("hydra_base_dir %s", config.get('DEFAULT', 'hydra_base_dir'))
        log.info("common_app_data_folder %s",
                 config.get('DEFAULT', 'common_app_data_folder'))
        log.info("win_common_documents %s",
                 config.get('DEFAULT', 'win_common_documents'))
        log.info("sqlite url %s", config.get('mysqld', 'url'))
        log.info("layout_xsd_path %s",
                 config.get('hydra_server', 'layout_xsd_path'))
        log.info("default_directory %s",
                 config.get('plugin', 'default_directory'))
        log.info("result_file %s", config.get('plugin', 'result_file'))
        log.info("plugin_xsd_path %s", config.get('plugin', 'plugin_xsd_path'))
        log.info("log_config_path %s",
                 config.get('logging_conf', 'log_config_path'))

        if port is None:
            port = config.getint('hydra_server', 'port', 8080)

        domain = config.get('hydra_server', 'domain', '127.0.0.1')

        check_port_available(domain, port)

        spyne.const.xml_ns.DEFAULT_NS = 'soap_server.hydra_complexmodels'
        cp_wsgi_application = Server((domain, port),
                                     application,
                                     numthreads=10)

        log.info("listening to http://%s:%s", domain, port)
        log.info("wsdl is at: http://%s:%s/soap/?wsdl", domain, port)

        try:
            cp_wsgi_application.start()
        except KeyboardInterrupt:
            cp_wsgi_application.stop()
示例#15
0
文件: sp.py 项目: SUNET/pysaml2
    POLICY = service_conf.POLICY

    add_urls()
    sign_alg = None
    digest_alg = None
    try:
        sign_alg = service_conf.SIGN_ALG
    except:
        pass
    try:
        digest_alg = service_conf.DIGEST_ALG
    except:
        pass
    ds.DefaultSignature(sign_alg, digest_alg)

    SRV = WSGIServer((HOST, PORT), ToBytesMiddleware(application))

    _https = ""
    if service_conf.HTTPS:
        SRV.ssl_adapter = pyopenssl.pyOpenSSLAdapter(
            SERVER_CERT, SERVER_KEY, CERT_CHAIN
        )
        _https = " using SSL/TLS"
    logger.info("Server starting")
    print("SP listening on %s:%s%s" % (HOST, PORT, _https))
    try:
        SRV.start()
    except KeyboardInterrupt:
        SRV.stop()
示例#16
0
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()
示例#17
0
def main():
    # Set null device file path to stdout, stdin, stderr if they are None
    for _name in ('stdin', 'stdout', 'stderr'):
        if getattr(sys, _name) is None:
            setattr(sys, _name,
                    open(os.devnull, 'r' if _name == 'stdin' else 'w'))

    # Build Javascript files when DEBUG
    if config.DEBUG:
        from pgadmin.utils.javascript.javascript_bundler import \
            JavascriptBundler, JsState
        app.debug = True

        javascript_bundler = JavascriptBundler()
        javascript_bundler.bundle()
        if javascript_bundler.report() == JsState.NONE:
            app.logger.error(
                "Unable to generate javascript.\n"
                "To run the app ensure that yarn install command runs "
                "successfully")
            raise RuntimeError("No generated javascript, aborting")

    # Output a startup message if we're not under the runtime and startup.
    # If we're under WSGI, we don't need to worry about this
    if not app.PGADMIN_RUNTIME:
        print("Starting %s. Please navigate to http://%s:%d in your browser." %
              (config.APP_NAME, config.DEFAULT_SERVER,
               config.EFFECTIVE_SERVER_PORT))
        sys.stdout.flush()
    else:
        # For unknown reason the runtime does not pass the environment
        # variables (i.e. PYTHONHOME, and PYTHONPATH), to the Python
        # sub-processes, leading to failures executing background processes.
        #
        # This has been observed only on windows. On *nix systems, it is likely
        # picking the system python environment, which is good enough to run
        # the process-executor.
        #
        # Setting PYTHONHOME launch them properly.
        from pgadmin.utils import IS_WIN

        if IS_WIN:
            os.environ['PYTHONHOME'] = sys.prefix

    # Initialize Flask service only once
    # If `WERKZEUG_RUN_MAIN` is None, i.e: app is initializing for first time
    # so set `use_reloader` = False, thus reload won't call.
    # Reference:
    # https://github.com/pallets/werkzeug/issues/220#issuecomment-11176538
    try:
        if config.DEBUG:
            app.run(host=config.DEFAULT_SERVER,
                    port=config.EFFECTIVE_SERVER_PORT,
                    use_reloader=((not app.PGADMIN_RUNTIME) and app.debug
                                  and os.environ.get("WERKZEUG_RUN_MAIN")
                                  is not None),
                    threaded=config.THREADED_MODE)
        else:
            # Can use cheroot instead of flask dev server when not in debug
            # 10 is default thread count in CherootServer
            num_threads = 10 if config.THREADED_MODE else 1
            prod_server = CherootServer(
                (config.DEFAULT_SERVER, config.EFFECTIVE_SERVER_PORT),
                wsgi_app=app,
                numthreads=num_threads,
                server_name=config.APP_NAME)
            try:
                print("Using production server...")
                prod_server.start()
            except KeyboardInterrupt:
                prod_server.stop()

    except IOError:
        app.logger.error("Error starting the app server: %s", sys.exc_info())
示例#18
0
class BitcoinRpcProxy(object):
    def __init__(self, bitcoind, rpcport=0):
        self.app = Flask("BitcoindProxy")
        self.app.add_url_rule("/", "API entrypoint", self.proxy, methods=['POST'])
        self.rpcport = rpcport
        self.mocks = {}
        self.mock_counts = {}
        self.bitcoind = bitcoind
        self.request_count = 0

    def _handle_request(self, r):
        conf_file = os.path.join(self.bitcoind.bitcoin_dir, 'bitcoin.conf')
        brpc = BitcoinProxy(btc_conf_file=conf_file)
        method = r['method']

        # If we have set a mock for this method reply with that instead of
        # forwarding the request.
        if method in self.mocks and type(method) == dict:
            self.mock_counts[method] += 1
            return self.mocks[method]
        elif method in self.mocks and callable(self.mocks[method]):
            self.mock_counts[method] += 1
            return self.mocks[method](r)

        try:
            reply = {
                "result": brpc._call(r['method'], *r['params']),
                "error": None,
                "id": r['id']
            }
        except JSONRPCError as e:
            reply = {
                "error": e.error,
                "code": -32603,
                "id": r['id']
            }
        self.request_count += 1
        return reply

    def proxy(self):
        r = json.loads(request.data.decode('ASCII'))

        if isinstance(r, list):
            reply = [self._handle_request(subreq) for subreq in r]
        else:
            reply = self._handle_request(r)

        response = flask.Response(json.dumps(reply, cls=DecimalEncoder))
        response.headers['Content-Type'] = 'application/json'
        return response

    def start(self):
        d = PathInfoDispatcher({'/': self.app})
        self.server = Server(('0.0.0.0', self.rpcport), d)
        self.proxy_thread = threading.Thread(target=self.server.start)
        self.proxy_thread.daemon = True
        self.proxy_thread.start()

        # Now that bitcoind is running on the real rpcport, let's tell all
        # future callers to talk to the proxyport. We use the bind_addr as a
        # signal that the port is bound and accepting connections.
        while self.server.bind_addr[1] == 0:
            pass
        self.rpcport = self.server.bind_addr[1]
        logging.debug("BitcoinRpcProxy proxying incoming port {} to {}".format(self.rpcport, self.bitcoind.rpcport))

    def stop(self):
        self.server.stop()
        self.proxy_thread.join()
        logging.debug("BitcoinRpcProxy shut down after processing {} requests".format(self.request_count))

    def mock_rpc(self, method, response=None):
        """Mock the response to a future RPC call of @method

        The response can either be a dict with the full JSON-RPC response, or a
        function that returns such a response. If the response is None the mock
        is removed and future calls will be passed through to bitcoind again.

        """
        if response is not None:
            self.mocks[method] = response
            self.mock_counts[method] = 0
        elif method in self.mocks:
            del self.mocks[method]
class BitcoinRpcProxy(object):
    def __init__(self, bitcoind, rpcport=0):
        self.app = Flask("BitcoindProxy")
        self.app.add_url_rule("/",
                              "API entrypoint",
                              self.proxy,
                              methods=['POST'])
        self.rpcport = rpcport
        self.mocks = {}
        self.mock_counts = {}
        self.bitcoind = bitcoind
        self.request_count = 0

    def _handle_request(self, r):
        conf_file = os.path.join(self.bitcoind.bitcoin_dir, 'bitcoin.conf')
        brpc = BitcoinProxy(btc_conf_file=conf_file)
        method = r['method']

        # If we have set a mock for this method reply with that instead of
        # forwarding the request.
        if method in self.mocks and type(method) == dict:
            self.mock_counts[method] += 1
            return self.mocks[method]
        elif method in self.mocks and callable(self.mocks[method]):
            self.mock_counts[method] += 1
            return self.mocks[method](r)

        try:
            reply = {
                "result": brpc._call(r['method'], *r['params']),
                "error": None,
                "id": r['id']
            }
        except JSONRPCError as e:
            reply = {"error": e.error, "code": -32603, "id": r['id']}
        self.request_count += 1
        return reply

    def proxy(self):
        r = json.loads(request.data.decode('ASCII'))

        if isinstance(r, list):
            reply = [self._handle_request(subreq) for subreq in r]
        else:
            reply = self._handle_request(r)

        response = flask.Response(json.dumps(reply, cls=DecimalEncoder))
        response.headers['Content-Type'] = 'application/json'
        return response

    def start(self):
        d = PathInfoDispatcher({'/': self.app})
        self.server = Server(('0.0.0.0', self.rpcport), d)
        self.proxy_thread = threading.Thread(target=self.server.start)
        self.proxy_thread.daemon = True
        self.proxy_thread.start()

        # Now that bitcoind is running on the real rpcport, let's tell all
        # future callers to talk to the proxyport. We use the bind_addr as a
        # signal that the port is bound and accepting connections.
        while self.server.bind_addr[1] == 0:
            pass
        self.rpcport = self.server.bind_addr[1]
        logging.debug("BitcoinRpcProxy proxying incoming port {} to {}".format(
            self.rpcport, self.bitcoind.rpcport))

    def stop(self):
        self.server.stop()
        self.proxy_thread.join()
        logging.debug(
            "BitcoinRpcProxy shut down after processing {} requests".format(
                self.request_count))

    def mock_rpc(self, method, response=None):
        """Mock the response to a future RPC call of @method

        The response can either be a dict with the full JSON-RPC response, or a
        function that returns such a response. If the response is None the mock
        is removed and future calls will be passed through to bitcoind again.

        """
        if response is not None:
            self.mocks[method] = response
            self.mock_counts[method] = 0
        elif method in self.mocks:
            del self.mocks[method]
示例#20
0
class ProxiedBitcoinD(BitcoinD):
    def __init__(self, bitcoin_dir, proxyport=0):
        BitcoinD.__init__(self, bitcoin_dir, rpcport=None)
        self.app = Flask("BitcoindProxy")
        self.app.add_url_rule("/", "API entrypoint", self.proxy, methods=["POST"])
        self.proxyport = proxyport
        self.mocks = {}

    def _handle_request(self, r):
        conf_file = os.path.join(self.bitcoin_dir, "bitcoin.conf")
        brpc = BitcoinProxy(btc_conf_file=conf_file)
        method = r["method"]

        # If we have set a mock for this method reply with that instead of
        # forwarding the request.
        if method in self.mocks and type(method) == dict:
            return self.mocks[method]
        elif method in self.mocks and callable(self.mocks[method]):
            return self.mocks[method](r)

        try:
            reply = {
                "result": brpc._call(r["method"], *r["params"]),
                "error": None,
                "id": r["id"],
            }
        except JSONRPCError as e:
            reply = {"error": e.error, "id": r["id"]}
        return reply

    def proxy(self):
        r = json.loads(request.data.decode("ASCII"))

        if isinstance(r, list):
            reply = [self._handle_request(subreq) for subreq in r]
        else:
            reply = self._handle_request(r)

        reply = json.dumps(reply, cls=DecimalEncoder)
        logging.debug("Replying to %r with %r", r, reply)

        response = flask.Response(reply)
        response.headers["Content-Type"] = "application/json"
        return response

    def start(self):
        d = PathInfoDispatcher({"/": self.app})
        self.server = Server(("0.0.0.0", self.proxyport), d)
        self.proxy_thread = threading.Thread(target=self.server.start)
        self.proxy_thread.daemon = True
        self.proxy_thread.start()
        BitcoinD.start(self)

        # Now that bitcoind is running on the real rpcport, let's tell all
        # future callers to talk to the proxyport. We use the bind_addr as a
        # signal that the port is bound and accepting connections.
        while self.server.bind_addr[1] == 0:
            pass
        self.proxiedport = self.rpcport
        self.rpcport = self.server.bind_addr[1]
        logging.debug(
            "bitcoind reverse proxy listening on {}, forwarding to {}".format(
                self.rpcport, self.proxiedport
            )
        )

    def stop(self):
        BitcoinD.stop(self)
        self.server.stop()
        self.proxy_thread.join()

    def mock_rpc(self, method, response=None):
        """Mock the response to a future RPC call of @method

        The response can either be a dict with the full JSON-RPC response, or a
        function that returns such a response. If the response is None the mock
        is removed and future calls will be passed through to bitcoind again.

        """
        if response is not None:
            self.mocks[method] = response
        elif method in self.mocks:
            del self.mocks[method]
示例#21
0
class GUIServer:
    def __init__(self,
                 shutdown_handler,
                 host='localhost',
                 port=5000,
                 ssl=False):
        csp = {
            'default-src': ['\'self\'', 'localhost'],
            'style-src': [
                '\'self\'', 'use.fontawesome.com', 'fonts.googleapis.com',
                'fonts.gstatic.com',
                'http://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css',
                'https://cdnjs.cloudflare.com/ajax/libs/jvectormap/2.0.4/jquery-jvectormap.css',
                'http://cdn.jsdelivr.net/chartist.js/latest/chartist.min.css',
                '\'unsafe-inline\''
            ],
            'font-src': [
                '\'self\'', 'use.fontawesome.com', 'fonts.googleapis.com',
                'fonts.gstatic.com', '\'unsafe-inline\''
            ],
            'img-src': [
                '\'self\'', 'data:', 'use.fontawesome.com',
                'fonts.googleapis.com', 'fonts.gstatic.com'
            ],
            'script-src': [
                '\'self\'', '\'unsafe-inline\'',
                'http://cdn.jsdelivr.net/chartist.js/latest/chartist.min.js'
            ],
        }

        if FROZEN:
            Talisman(_app, force_https=ssl, content_security_policy=csp)
        else:
            from flask_cors import CORS
            CORS(_app)
        #
        # otp = OTP()
        # otp.init_app(_app)

        flask_compress.Compress(_app)

        self._shutdown_handler = shutdown_handler
        self._host = host
        self._port = int(port)

        self._use_ssl = ssl
        self._certfile_path = APP_DIR / 'lib' / 'liquitrader.crt'
        self._keyfile_path = APP_DIR / 'lib' / 'liquitrader.key'

        self._jwt = None

        # Set constants for WSGIServer
        WSGIServer.version = 'LiquiTrader/2.0'

        self._wsgi_server = None

    # ----
    def _create_self_signed_cert(self):
        # create a key pair
        k = crypto.PKey()
        k.generate_key(crypto.TYPE_RSA, 2048)

        # create a self-signed cert
        cert = crypto.X509()
        cert.get_subject().C = 'US'
        cert.get_subject().O = 'LiquiTrader'
        cert.get_subject().CN = 'localhost'
        cert.set_serial_number(int(binascii.hexlify(os.urandom(16)), 16))
        cert.gmtime_adj_notBefore(0)
        cert.gmtime_adj_notAfter(10 * 365 * 24 * 60 * 60)
        cert.set_issuer(cert.get_subject())
        cert.set_pubkey(k)
        cert.sign(k, 'sha256')

        with open(self._certfile_path, 'wb') as certfile:
            certfile.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))

        with open(self._keyfile_path, 'wb') as keyfile:
            keyfile.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k))

    # --
    def _init_ssl(self):
        import flask_sslify
        flask_sslify.SSLify(_app, permanent=True)

        if not (os.path.exists(self._certfile_path)
                and os.path.exists(self._keyfile_path)):
            self._create_self_signed_cert()

        WSGIServer.ssl_adapter = BuiltinSSLAdapter(
            certificate=self._certfile_path, private_key=self._keyfile_path)

    # ----
    def run(self):
        import utils.runtime_handler
        utils.runtime_handler.enable_traceback_hook(
        )  # Enable custom traceback handling (to strip build path info)

        self._shutdown_handler.add_task()

        if self._use_ssl:
            self._init_ssl()

        ks = _KeyStore.query.first()
        if ks is not None:
            _app.config['SECRET_KEY'] = ks.flask_secret
        else:
            # Temporary static key for first run. We should roll this occasionally.
            _app.config[
                'SECRET_KEY'] = b'\xee\xf0\xabc>\xc8\xa4S\xa1\x89\xff\xa3\xaf\xcfX\xac'

        self._jwt = JWT(_app, user_authenticate, user_identity)

        self._wsgi_server = WSGIServer((self._host, int(self._port)),
                                       PathInfoDispatcher({'/': _app}))

        # TODO: Issue with SSL:
        # Firefox causes a (socket.error 1) exception to be thrown on initial connection (before accepting cert)
        # This is internal to cheroot, so it may be difficult to handle

        print('LiquiTrader is ready for action!')
        print(
            f'Visit http{"s" if self._use_ssl else ""}://{self._host}:{self._port} in your favorite browser'
        )
        print('to start trading!')

        self._wsgi_server.start()

    # ----
    def stop(self):
        self._wsgi_server.stop()
        self._shutdown_handler.remove_task()
示例#22
0
文件: app.py 项目: andykee/aurora
def main(argv=sys.argv[1:]):

    parser = argparse.ArgumentParser(
        description='Start Aurora on http://<host>:<port>')

    parser.add_argument(
        dest='input',
        nargs='?',
        help='Path where to find Aurora content or catalog file',
        default='.')

    parser.add_argument('--live',
                        dest='live',
                        help='Run Aurora in live mode',
                        action='store_true')

    parser.add_argument('-p',
                        '--port',
                        dest='port',
                        help='Port to serve HTTP files at (default: 5000)',
                        default=PORT)

    parser.add_argument('-H',
                        '--host',
                        dest='host',
                        help='IP to serve HTTP files at (default: localhost)',
                        default=HOST)

    args = parser.parse_args(argv)

    # The default behavior should be as follows:
    # * If the user provides a catalog file to open, open it
    # * If the user does not provide a catalog file to open:
    #     * Webapp should prompt them to either open a file
    #       or specify the location for a new one.
    #
    # In the meantime, we'll just create one in memory

    if os.path.isfile(args.input):
        # User provided a catalog file
        engine = new_engine(catalog=args.input)
    else:
        engine = new_engine()

    session = new_session(engine)
    Base.metadata.create_all(bind=engine)

    api.session = scoped_session(session, scopefunc=_app_ctx_stack)

    # We need to read the config from the db here to figure out what to do re: a bunch of stuff

    server = Server((args.host, args.port), app)

    if args.live:
        print("F**k it, we'll do it live")
    else:
        pass

    # Stand up the webserver
    print(f'Running Aurora on http://{args.host}:{args.port}')

    try:
        server.start()
        if CONFIG['autolaunch_browser']:
            webbrowser.open(f'http://{HOST}:{PORT}', new=2)
    except KeyboardInterrupt:
        print(f'\n\nShutting down Aurora')
        server.stop()
示例#23
0
    sign_alg = None
    digest_alg = None
    try:
        sign_alg = CONFIG.SIGN_ALG
    except AttributeError:
        pass
    try:
        digest_alg = CONFIG.DIGEST_ALG
    except AttributeError:
        pass
    ds.DefaultSignature(sign_alg, digest_alg)

    SRV = WSGIServer((HOST, PORT), application)

    _https = ""
    if CONFIG.HTTPS:
        https = "using HTTPS"
        # SRV.ssl_adapter = ssl_pyopenssl.pyOpenSSLAdapter(
        #     config.SERVER_CERT, config.SERVER_KEY, config.CERT_CHAIN)
        SRV.ssl_adapter = BuiltinSSLAdapter(CONFIG.SERVER_CERT,
                                            CONFIG.SERVER_KEY,
                                            CONFIG.CERT_CHAIN)

    logger.info("Server starting")
    print("IDP listening on %s:%s%s" % (HOST, PORT, _https))
    try:
        SRV.start()
    except KeyboardInterrupt:
        SRV.stop()
示例#24
0
class CherryProxy:
    """
    CherryProxy: a filtering HTTP proxy.
    """

    # class variables: unique id for each request
    _req_id = 0
    _lock_req_id = threading.Lock()

    def __init__(self,
                 listen_address,
                 listen_port,
                 redirect_address,
                 redirect_port,
                 verbosity,
                 log_stream=sys.stdout):
        self.listen_address = listen_address
        self.listen_port = listen_port
        self.redirect_address = redirect_address
        self.redirect_port = redirect_port
        self._verbosity = verbosity
        self._log_stream = log_stream

        # thread local variables to store request/response data per thread:
        self.req = threading.local()
        self.resp = threading.local()

        self.server = CherryPyWSGIServer(
            (self.listen_address, self.listen_port), self._proxy_app)
        self.log_debug('__init__',
                       'server.bind_addr: {}'.format(self.server.bind_addr))

    def log(self, s, prefix='### ', req_id=None):
        if self._verbosity > 0:
            if req_id:
                prefix += '{:06} | '.format(req_id)
            self._log_stream.write('{}{}\n'.format(prefix, s))

    def log_debug(self, method_name, s):
        if self._verbosity > 1:
            options = {
                's': '{:25s} | {}'.format(method_name, s),
                'prefix': '*** ',
            }
            if hasattr(self.req, 'id'):
                options['req_id'] = self.req.id
            self.log(**options)

    def start(self):
        self.log('Starting proxy to listen on {} and redirect to {}'.format(
            self.server.bind_addr,
            (self.redirect_address, self.redirect_port)))
        self.log('Proxy listening ... (press Ctrl+C to stop)'.format(
            self.server.bind_addr))
        self.server.start()

    def stop(self):
        self.server.stop()
        self.log('Proxy stopped.')

    def set_response(self,
                     status,
                     reason=None,
                     data=None,
                     content_type='text/plain'):
        """
        Set a HTTP response to be sent to the client instead of the one from the server.
        :param status: int, HTTP status code (see RFC 2616)
        :param reason: str, optional text for the response line, standard text by default
        :param data: str, optional body for the response, default="status reason"
        :param content_type: str, content-type corresponding to data
        :return:
        """
        self.resp.status = status

        if not reason:
            reason = client.responses[
                status]  # get standard text corresponding to status
        self.resp.reason = reason

        if not data:
            data = '{} {}'.format(status, reason)
        self.resp.data = data

        # reset all headers
        self.resp.headers = []
        self.resp.headers.append(('content-type', content_type))
        # self.resp.headers.append(('content-length', str(len(data))))

    def set_response_forbidden(self):
        self.set_response(403, reason='Forbidden')

    def filter_request_headers(self):
        """
        Method to be overridden:
        Called to analyse/filter/modify the request received from the client,
        before reading the full request with its body if there is one,
        before it is sent to the server.

        This method may call set_response() if the request needs to be blocked
        before being sent to the server.

        The following attributes can be read and MODIFIED:
            self.req.headers: dictionary of HTTP headers, with lowercase names
            self.req.method: HTTP method, e.g. 'GET', 'POST', etc
            self.req.scheme: protocol from URL, e.g. 'http' or 'https'
            self.req.path: path in URL, for example '/folder/index.html'
            self.req.query: query string, found after question mark in URL

        The following attributes can be READ only:
            self.req.environ: dictionary of request attributes following WSGI format (PEP 333)
            self.req.url: partial URL containing 'path?query'
            self.req.content_type: content-type, for example 'text/html'
            self.req.charset: charset, for example 'UTF-8'
            self.req.length: length of request data in bytes, 0 if none
        """
        pass

    def filter_request(self):
        """
        Method to be overridden:
        Called to analyse/filter/modify the request received from the client,
        after reading the full request with its body if there is one,
        before it is sent to the server.

        This method may call set_response() if the request needs to be blocked
        before being sent to the server.

        The same attributes as in filter_request_headers are available:

        The following attributes can also be read and MODIFIED:
            self.req.data: data sent with the request (POST or PUT)
        """
        pass

    def filter_response_headers(self):
        """
        Method to be overridden:
        Called to analyse/filter/modify the response received from the server,
        before reading the full response with its body if there is one,
        before it is sent back to the client.

        This method may call set_response() if the response needs to be blocked
        (e.g. replaced by a simple response) before being sent to the client.

        The following attributes can be read and MODIFIED:
            self.resp.status: int, HTTP status of response, e.g. 200, 404, etc
            self.resp.reason: reason string, e.g. 'OK', 'Not Found', etc
            self.resp.headers: response headers, list of (header, value) tuples

        The following attributes can be READ only:
            self.resp.httpconn: httplib.HTTPConnection object
            self.resp.response: httplib.HTTPResponse object
            self.resp.content_type: content-type of response
        """
        pass

    def filter_response(self):
        """
        Method to be overridden:
        Called to analyse/filter/modify the response received from the server,
        after reading the full response with its body if there is one,
        before it is sent back to the client.

        This method may call set_response() if the response needs to be blocked
        (e.g. replaced by a simple response) before being sent to the client.

        The same attributes as in filter_response_headers are available:

        The following attributes can be read and MODIFIED:
            self.resp.data: data sent with the response
        """
        pass

    def _proxy_app(self, environ, f_start_response):
        """
        Main method called when a request is received from a client (WSGI application).
        :param environ:
        :param f_start_response:
        :return:
        """
        t_start = time.perf_counter()
        self._init_request_response()
        self.log('START', req_id=self.req.id)

        self._parse_request(environ)

        # method to be overridden by subclass: filter request headers before reading the body
        self.filter_request_headers()

        if not self.resp.status:
            self._read_request_body()

            # method to be overridden by subclass: filter request before sending it to the server
            self.filter_request()

        self.log('request {} {}'.format(self.req.method, self.req.url),
                 req_id=self.req.id)

        if not self.resp.status:
            self._send_request()
            self._parse_response()

            # method to be overridden by subclass: filter response headers before reading the body
            self.filter_response_headers()

        if not self.resp.data:  # here we need to check resp.data
            self._read_response_body()

            # method to be overridden by subclass: filter request before sending it to the client
            self.filter_response()

        # for now we always close the connection, even if the client sends several requests;
        # this is not optimal performance-wise, but simpler to code
        if self.resp.httpconn:
            self.resp.httpconn.close()

        self._send_response(f_start_response)

        t_end = time.perf_counter()
        self.log('response {} {} in {:5.3f} seconds'.format(
            self.resp.status, self.resp.reason, t_end - t_start),
                 req_id=self.req.id)
        return [self.resp.data]

    def _init_request_response(self):
        # set request id (simply increase number at each request)
        with self._lock_req_id:
            self._req_id += 1
            self.req.id = self._req_id

        # request variables
        self.req.environ = {}
        self.req.headers = {}
        self.req.method = None
        self.req.scheme = None
        self.req.path = None
        self.req.query = None
        self.req.url = None
        self.req.content_type = None
        self.req.charset = None
        self.req.length = 0
        self.req.data = None

        # response variables
        self.resp.httpconn = None
        self.resp.response = None
        self.resp.status = None
        self.resp.reason = None
        self.resp.headers = [
        ]  # http.client headers is a list of (header, value) tuples
        self.resp.content_type = None
        self.resp.data = None

    def _parse_request(self, environ):
        self.req.environ = environ
        self.log_debug('_parse_request', 'req.environ:')
        for k, v in sorted(environ.items(), key=lambda x: x[0]):
            self.log_debug('_parse_request', '   {}: {}'.format(k, v))

        # convert WSGI environ to a dict of HTTP headers:
        self.req.headers = {}
        for h in environ:
            if h.startswith('HTTP_'):
                self.req.headers[h[5:].replace('_', '-').lower()] = environ[h]

        # content-type is stored without 'HTTP_'
        # see RFC 2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17
        content_type = environ.get('CONTENT_TYPE', None)
        if content_type:
            if ';' in content_type:
                content_type, charset = content_type.split(';', 1)
                self.req.content_type = content_type.strip()
                self.req.charset = charset.strip()
                ct = '{};{}'.format(self.req.content_type, self.req.charset)
            else:
                self.req.content_type = content_type.strip()
                ct = self.req.content_type
            self.req.headers['content-type'] = ct
        self.log_debug('_parse_request',
                       'req.content_type: {}'.format(self.req.content_type))
        self.log_debug('_parse_request',
                       'req.charset: {}'.format(self.req.charset))

        # content-length is also stored without 'HTTP_'
        self.req.headers['content-length'] = environ.get('CONTENT_LENGTH', 0)
        self.log_debug('_parse_request', 'req.headers:')
        for k, v in sorted(self.req.headers.items(), key=lambda x: x[0]):
            self.log_debug('_parse_request', '   {}: {}'.format(k, v))

        self.req.method = environ.get('REQUEST_METHOD', None)
        self.req.scheme = environ.get('wsgi.url_scheme', None)  # http
        self.req.path = environ.get('PATH_INFO', None)
        self.req.query = environ.get('QUERY_STRING', None)
        self.req.url = parse.urlunsplit(
            ('', '', self.req.path, self.req.query, ''))
        self.log_debug('_parse_request',
                       'req.method: {}'.format(self.req.method))
        self.log_debug('_parse_request',
                       'req.scheme: {}'.format(self.req.scheme))
        self.log_debug('_parse_request', 'req.path: {}'.format(self.req.path))
        self.log_debug('_parse_request',
                       'req.query: {}'.format(self.req.query))
        self.log_debug('_parse_request', 'req.url: {}'.format(self.req.url))

        if self.req.method not in ALLOWED_METHODS:
            # here I use 501 "not implemented" rather than 405 or 401, because it seems to be the
            # most appropriate response according to RFC 2616.
            # see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
            msg = 'Method "{}" not supported.'.format(self.req.method)
            self.set_response(501, reason=msg)
            self.log_debug('_parse_request', msg)

        if self.req.scheme not in ALLOWED_SCHEMES:
            msg = 'Scheme "{}" not supported.'.format(self.req.scheme)
            self.set_response(501, reason=msg)
            self.log_debug('_parse_request', msg)

    def _read_request_body(self):
        try:
            length = int(self.req.environ.get('CONTENT_LENGTH', 0))
        except ValueError:
            length = 0

        if length > 0:
            self.req.length = length
            input_ = self.req.environ.get('wsgi.input')
            if input_:
                self.req.data = input_.read(self.req.length)
                self.log_debug('_read_request_body',
                               'req.length: {}'.format(self.req.length))
                self.log_debug('_read_request_body',
                               'req.data: {} b'.format(len(self.req.data)))
                return

        self.log_debug('_read_request_body', 'No request body')

    def _send_request(self):
        self.log_debug('_send_request', 'starting')

        # forward a request received from a client to the server.
        # TO DO: handle connection errors
        self.resp.httpconn = client.HTTPConnection(self.redirect_address,
                                                   self.redirect_port)
        self.log_debug('_send_request', 'initialized connection')

        self.resp.httpconn.request(self.req.method,
                                   self.req.url,
                                   body=self.req.data,
                                   headers=self.req.headers)
        self.log_debug('_send_request', 'made request')

        # Get the response (but not the response body yet).
        self.resp.response = self.resp.httpconn.getresponse()
        self.log_debug('_send_request', 'got response')

    def _parse_response(self):
        self.resp.status = self.resp.response.status
        self.resp.reason = self.resp.response.reason
        self.log_debug(
            '_parse_response', 'resp: {} {}'.format(self.resp.status,
                                                    self.resp.reason))

        self.resp.headers = self.resp.response.getheaders()
        self.log_debug('_parse_response', 'resp.headers:')
        for e in sorted(self.resp.headers):
            self.log_debug('_parse_response', '   {}: {}'.format(e[0], e[1]))

        self.resp.content_type = self.resp.response.msg.get_content_type(
        ).lower()
        self.log_debug('_parse_response',
                       'resp.content_type: {}'.format(self.resp.content_type))

    def _read_response_body(self):
        # TO DO: check content-length?
        self.resp.data = self.resp.response.read()
        self.log_debug('_read_response_body',
                       'resp.data: {} b'.format(len(self.resp.data)))

    def _send_response(self, f_start_response):
        # Send the response with headers (but no body yet).
        status = '{} {}'.format(self.resp.status, self.resp.reason)
        f_start_response(status, self.resp.headers)
        self.log_debug('_send_response', 'status: {}'.format(status))
示例#25
0
def main():
    parser = argparse.ArgumentParser(description='Example OIDC Provider.')
    parser.add_argument("-p", "--port", default=80, type=int)
    parser.add_argument("-b", "--base", default="https://0.0.0.0", type=str)
    parser.add_argument("-d", "--debug", action="store_true")
    parser.add_argument("settings")
    args = parser.parse_args()

    # Load configuration
    with open(args.settings, "r") as f:
        settings = yaml.load(f)

    issuer = args.base.rstrip("/")

    template_dirs = settings["server"].get("template_dirs", "templates")
    jinja_env = Environment(loader=FileSystemLoader(template_dirs))
    authn_broker, auth_routing = setup_authentication_methods(
        settings["authn"], jinja_env)

    # Setup userinfo
    userinfo_conf = settings["userinfo"]
    cls = make_cls_from_name(userinfo_conf["class"])
    i = cls(**userinfo_conf["kwargs"])
    userinfo = UserInfo(i)

    client_db = {}
    session_db = create_session_db(issuer,
                                   secret=rndstr(32),
                                   password=rndstr(32))
    provider = Provider(issuer,
                        session_db,
                        client_db,
                        authn_broker,
                        userinfo,
                        AuthzHandling(),
                        verify_client,
                        None,
                        extra_claims=["faculty", "is_rich", "profyle_member"],
                        extra_scope_dict={
                            'profile': [
                                "is_rich", "faculty", "profyle_member",
                                "given_name", "_claim_names", "_claim_sources"
                            ],
                            'roles': ["faculty", "profyle_member"]
                        })
    provider.baseurl = issuer
    provider.symkey = rndstr(16)

    # Setup keys
    path = os.path.join(os.path.dirname(__file__), "static")
    try:
        os.makedirs(path)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise e
        pass
    jwks = keyjar_init(provider, settings["provider"]["keys"])
    name = "jwks.json"
    with open(os.path.join(path, name), "w") as f:
        f.write(json.dumps(jwks))

    # Mount the WSGI callable object (app) on the root directory
    app_routing = setup_endpoints(provider)
    app_routing["/.well-known/openid-configuration"] = pyoidcMiddleware(
        provider.providerinfo_endpoint)
    app_routing["/.well-known/webfinger"] = pyoidcMiddleware(
        partial(_webfinger, provider))
    routing = dict(list(auth_routing.items()) + list(app_routing.items()))
    routing["/static"] = make_static_handler(path)
    dispatcher = WSGIPathInfoDispatcher(routing)
    server = WSGIServer(('0.0.0.0', args.port), dispatcher)
    server.ssl_adapter = BuiltinSSLAdapter(settings["server"]["cert"],
                                           settings["server"]["key"],
                                           settings["server"]["cert_chain"])

    # Start the CherryPy WSGI web server
    try:
        print("Server started: {}".format(issuer))
        server.start()
    except KeyboardInterrupt:
        server.stop()
示例#26
0
my_app = WSGIPathInfoDispatcher({'/': app})

#                    'localhost'        privat, bare lokal
#                    '127.0.0.1'        privat, bare lokal
#                    '192.168.0.37'     offentlig, tilgjengelig for andre på nettverket (eksempel IPv4 addresse, sett inn din egen)
#                    '0.0.0.0'          offentlig, tilgjengelig for andre på nettverket (alle IP addressene til maskinen kan brukes for å nå siden)
server = WSGIServer(('0.0.0.0', 443), my_app)   # For å åpne denne serveren må en skrive https:// forran addressen

ssl_cert = "application/certificate/MyCertificate.crt"
ssl_key = "application/certificate/MyKey.key"
server.ssl_adapter =  BuiltinSSLAdapter(ssl_cert, ssl_key, None)

if __name__ == '__main__':
   try:
      server.start()
   except KeyboardInterrupt:  # Med dette så mener den ctrl+c
      server.stop()                                            # På linje 2070 i koden hvor stop() er definert har jeg satt inn: self.serving = False  # We don't care, fordi koden fryser der, er noe feil med cheroot
   except SystemExit:
      server.stop()                                            # På linje 2070 i koden hvor stop() er definert har jeg satt inn: self.serving = False  # We don't care, fordi koden fryser der, er noe feil med cheroot

# TLS test
# https://www.ssllabs.com/ssltest/index.html

# https://blog.miguelgrinberg.com/post/running-your-flask-application-over-https
# req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365

# https://www.linode.com/docs/security/ssl/create-a-self-signed-tls-certificate/
# req -new -newkey rsa:4096 -x509 -sha256 -days 100 -nodes -out MyCertificate.crt -keyout MyKey.key

# https://stackoverflow.com/questions/10175812/how-to-create-a-self-signed-certificate-with-openssl
示例#27
0
文件: cli.py 项目: yegle/fava
def main(
    filenames: tuple[str],
    port: int,
    host: str,
    prefix: str,
    incognito: bool,
    debug: bool,
    profile: bool,
    profile_dir: str,
) -> None:  # pragma: no cover
    """Start Fava for FILENAMES on http://<host>:<port>.

    If the `BEANCOUNT_FILE` environment variable is set, Fava will use the
    files (space-delimited) specified there in addition to FILENAMES.

    Note you can also specify command-line options via environment variables.
    For example, `--host=0.0.0.0` is equivalent to setting the environment
    variable `FAVA_HOST=0.0.0.0`.
    """

    if profile:
        debug = True

    env_filename = os.environ.get("BEANCOUNT_FILE")
    all_filenames = (
        filenames + tuple(env_filename.split()) if env_filename else filenames
    )

    if not all_filenames:
        raise click.UsageError("No file specified")

    app.config["BEANCOUNT_FILES"] = all_filenames
    app.config["INCOGNITO"] = incognito

    if prefix:
        app.wsgi_app = DispatcherMiddleware(  # type: ignore
            simple_wsgi, {prefix: app.wsgi_app}
        )

    if host == "localhost":
        # ensure that cheroot does not use IP6 for localhost
        host = "127.0.0.1"

    click.echo(f"Starting Fava on http://{host}:{port}")
    if not debug:
        server = Server((host, port), app)
        try:
            server.start()
        except KeyboardInterrupt:
            click.echo("Keyboard interrupt received: stopping Fava", err=True)
            server.stop()
        except OSError as error:
            if "No socket could be created" in str(error):
                click.echo(
                    f"Cannot start Fava because port {port} is already in use."
                    "\nPlease choose a different port with the '-p' option."
                )
            raise click.Abort
    else:
        if profile:
            app.config["PROFILE"] = True
            app.wsgi_app = ProfilerMiddleware(  # type: ignore
                app.wsgi_app,
                restrictions=(30,),
                profile_dir=profile_dir if profile_dir else None,
            )

        app.jinja_env.auto_reload = True
        try:
            app.run(host, port, debug)
        except OSError as error:
            if error.errno == errno.EADDRINUSE:
                click.echo(
                    f"Cannot start Fava because port {port} is already in use."
                    "\nPlease choose a different port with the '-p' option."
                )
                raise click.Abort
            raise
示例#28
0
def cherrypy_server_runner(app,
                           global_conf=None,
                           host='127.0.0.1',
                           port=None,
                           ssl_pem=None,
                           protocol_version=None,
                           numthreads=None,
                           server_name=None,
                           max=None,
                           request_queue_size=None,
                           timeout=None):  # pragma: no cover
    """
    Entry point for CherryPy's WSGI server

    Serves the specified WSGI app via CherryPyWSGIServer.

    ``app``

        The WSGI 'application callable'; multiple WSGI applications
        may be passed as (script_name, callable) pairs.

    ``host``

        This is the ipaddress to bind to (or a hostname if your
        nameserver is properly configured).  This defaults to
        127.0.0.1, which is not a public interface.

    ``port``

        The port to run on, defaults to 8080 for HTTP, or 4443 for
        HTTPS. This can be a string or an integer value.

    ``ssl_pem``

        This an optional SSL certificate file (via OpenSSL) You can
        generate a self-signed test PEM certificate file as follows:

            $ openssl genrsa 1024 > host.key
            $ chmod 400 host.key
            $ openssl req -new -x509 -nodes -sha1 -days 365  \\
                          -key host.key > host.cert
            $ cat host.cert host.key > host.pem
            $ chmod 400 host.pem

    ``protocol_version``

        The protocol used by the server, by default ``HTTP/1.1``.

    ``numthreads``

        The number of worker threads to create.

    ``server_name``

        The string to set for WSGI's SERVER_NAME environ entry.

    ``max``

        The maximum number of queued requests. (defaults to -1 = no
        limit).

    ``request_queue_size``

        The 'backlog' argument to socket.listen(); specifies the
        maximum number of queued connections.

    ``timeout``

        The timeout in seconds for accepted connections.
    """
    is_ssl = False
    if ssl_pem:
        port = port or 4443
        is_ssl = True

    if not port:
        if ':' in host:
            host, port = host.split(':', 1)
        else:
            port = 8080
    bind_addr = (host, int(port))

    kwargs = {}
    for var_name in ('numthreads', 'max', 'request_queue_size', 'timeout'):
        var = locals()[var_name]
        if var is not None:
            kwargs[var_name] = int(var)

    try:
        from cheroot.wsgi import Server as WSGIServer
    except ImportError:
        from cherrypy.wsgiserver import CherryPyWSGIServer as WSGIServer

    server = WSGIServer(bind_addr, app, server_name=server_name, **kwargs)
    if ssl_pem is not None:
        if PY2:
            server.ssl_certificate = server.ssl_private_key = ssl_pem
        else:
            # creates wsgiserver.ssl_builtin as side-effect
            try:
                from cheroot.server import get_ssl_adapter_class
                from cheroot.ssl.builtin import BuiltinSSLAdapter
            except ImportError:
                from cherrypy.wsgiserver import get_ssl_adapter_class
                from cherrypy.wsgiserver.ssl_builtin import BuiltinSSLAdapter
            get_ssl_adapter_class()
            server.ssl_adapter = BuiltinSSLAdapter(ssl_pem, ssl_pem)

    if protocol_version:
        server.protocol = protocol_version

    try:
        protocol = is_ssl and 'https' or 'http'
        if host == '0.0.0.0':
            print('serving on 0.0.0.0:%s view at %s://127.0.0.1:%s' %
                  (port, protocol, port))
        else:
            print('serving on %s://%s:%s' % (protocol, host, port))
        server.start()
    except (KeyboardInterrupt, SystemExit):
        server.stop()

    return server
示例#29
0
文件: pserve.py 项目: JDeuce/pyramid
def cherrypy_server_runner(
        app, global_conf=None, host='127.0.0.1', port=None,
        ssl_pem=None, protocol_version=None, numthreads=None,
        server_name=None, max=None, request_queue_size=None,
        timeout=None
        ):  # pragma: no cover
    """
    Entry point for CherryPy's WSGI server

    Serves the specified WSGI app via CherryPyWSGIServer.

    ``app``

        The WSGI 'application callable'; multiple WSGI applications
        may be passed as (script_name, callable) pairs.

    ``host``

        This is the ipaddress to bind to (or a hostname if your
        nameserver is properly configured).  This defaults to
        127.0.0.1, which is not a public interface.

    ``port``

        The port to run on, defaults to 8080 for HTTP, or 4443 for
        HTTPS. This can be a string or an integer value.

    ``ssl_pem``

        This an optional SSL certificate file (via OpenSSL) You can
        generate a self-signed test PEM certificate file as follows:

            $ openssl genrsa 1024 > host.key
            $ chmod 400 host.key
            $ openssl req -new -x509 -nodes -sha1 -days 365  \\
                          -key host.key > host.cert
            $ cat host.cert host.key > host.pem
            $ chmod 400 host.pem

    ``protocol_version``

        The protocol used by the server, by default ``HTTP/1.1``.

    ``numthreads``

        The number of worker threads to create.

    ``server_name``

        The string to set for WSGI's SERVER_NAME environ entry.

    ``max``

        The maximum number of queued requests. (defaults to -1 = no
        limit).

    ``request_queue_size``

        The 'backlog' argument to socket.listen(); specifies the
        maximum number of queued connections.

    ``timeout``

        The timeout in seconds for accepted connections.
    """
    is_ssl = False
    if ssl_pem:
        port = port or 4443
        is_ssl = True

    if not port:
        if ':' in host:
            host, port = host.split(':', 1)
        else:
            port = 8080
    bind_addr = (host, int(port))

    kwargs = {}
    for var_name in ('numthreads', 'max', 'request_queue_size', 'timeout'):
        var = locals()[var_name]
        if var is not None:
            kwargs[var_name] = int(var)

    try:
        from cheroot.wsgi import Server as WSGIServer
    except ImportError:
        from cherrypy.wsgiserver import CherryPyWSGIServer as WSGIServer

    server = WSGIServer(bind_addr, app,
                        server_name=server_name, **kwargs)
    if ssl_pem is not None:
        if PY2:
            server.ssl_certificate = server.ssl_private_key = ssl_pem
        else:
            # creates wsgiserver.ssl_builtin as side-effect
            try:
                from cheroot.server import get_ssl_adapter_class
                from cheroot.ssl.builtin import BuiltinSSLAdapter
            except ImportError:
                from cherrypy.wsgiserver import get_ssl_adapter_class
                from cherrypy.wsgiserver.ssl_builtin import BuiltinSSLAdapter
            get_ssl_adapter_class()
            server.ssl_adapter = BuiltinSSLAdapter(ssl_pem, ssl_pem)

    if protocol_version:
        server.protocol = protocol_version

    try:
        protocol = is_ssl and 'https' or 'http'
        if host == '0.0.0.0':
            print('serving on 0.0.0.0:%s view at %s://127.0.0.1:%s' %
                  (port, protocol, port))
        else:
            print('serving on %s://%s:%s' % (protocol, host, port))
        server.start()
    except (KeyboardInterrupt, SystemExit):
        server.stop()

    return server
示例#30
0
class BitcoindRpcProxy(object):
    """A proxy to the bitcoind RPC interface that can replace commands with arbitrary results.

    Starts a HTTP server in a thread, listens for incoming JSONRPC requests, and responds with
    either a mocked result or the result it got from bitcoind.
    This was taken and adapted from the C-lightning test suite.
    """
    def __init__(self, bitcoind_rpc_port, bitcoind_cookie_path, mocks):
        self.app = Flask("BitcoindProxy")
        self.app.add_url_rule(
            "/",
            "Entrypoint",
            self.proxy,
            methods=["POST"],
            defaults={"path": ""},
        )
        self.app.add_url_rule(
            "/<path:path>",
            "Entrypoint",
            self.proxy,
            methods=["POST"],
        )
        self.rpcport = reserve()
        # A mapping from method name to result as a dict.
        # Eventually, the results could be callable.
        self.mocks = mocks
        self.bitcoind_rpc_port = bitcoind_rpc_port
        self.bitcoind_cookie_path = bitcoind_cookie_path

        self.start()

    def __del__(self):
        self.stop()

    def _handle_request(self, r, path):
        """Handle a JSONRPC request {r} made to the HTTP endpoint {path} (to handle
        wallet paths)"""
        method = r["method"]

        # If we have set a mock for this method reply with that
        if method in self.mocks:
            return {"id": r["id"], "error": None, "result": self.mocks[method]}

        # Otherwise, just forward the request
        with open(self.bitcoind_cookie_path) as fd:
            authpair = fd.read()
        service_url = f"http://{authpair}@localhost:{self.bitcoind_rpc_port}/{path}"

        try:
            res = AuthServiceProxy(service_url, r["method"])(*r["params"])
            return {"result": res, "id": r["id"]}
        except JSONRPCException as e:
            return {"error": e.error, "id": r["id"]}

    def proxy(self, path):
        r = json.loads(request.data.decode("ASCII"))

        if isinstance(r, list):
            reply = [self._handle_request(subreq, path) for subreq in r]
        else:
            reply = self._handle_request(r, path)

        # \r\n because rust-jsonrpc expects it..
        response = Response(json.dumps(reply, cls=DecimalEncoder) + "\r\n")
        response.headers["Content-Type"] = "application/json"
        return response

    def start(self):
        self.server = Server(
            ("127.0.0.1", self.rpcport),
            self.app,
            numthreads=32,
            request_queue_size=10,
            accepted_queue_timeout=20,
            timeout=TIMEOUT * 2,
        )
        self.proxy_thread = threading.Thread(target=self.server.start)
        self.proxy_thread.daemon = True
        self.proxy_thread.start()

        # Now that bitcoind is running on the real rpcport, let's tell all
        # future callers to talk to the proxyport. We use the bind_addr as a
        # signal that the port is bound and accepting connections.
        while self.server.bind_addr[1] == 0:
            pass
        self.rpcport = self.server.bind_addr[1]

    def stop(self):
        self.server.stop()
        self.proxy_thread.join()
示例#31
0
    received_data = request.get_json(force=True)
    extracted_df = jsonToCSV(received_data)
    # TODO: Change hope feature vector calls to respective function. Uncomment for debugging temporarily
    buy = feature_vector_buy(extracted_df, test=True)
    communicate = feature_vector_communicate(extracted_df, test=True)
    fun = feature_vector_fun(extracted_df, test=True)
    hope = feature_vector_hope(extracted_df, test=True)
    mother = feature_vector_mother(extracted_df, test=True)
    really = feature_vector_really(extracted_df, test=True)
    hope = feature_vector_hope(extracted_df, test=True)
    feature_vectors = [buy, communicate, fun, hope, mother, really]
    results = dict()
    for i in range(1, 5):
        scores = [0] * 6
        for gesture_id in INDEX_TO_GESTURE:
            scores[ord(gesture_id) - 65] = get_result_from_model(
                i, gesture_id, feature_vectors[ord(gesture_id) - 65])
        results[i] = INDEX_TO_GESTURE[chr(ord('A') + np.argmax(scores))]
    return jsonify(results)


if __name__ == '__main__':
    server = WSGIServer(bind_addr=('127.0.0.1', 9696),
                        wsgi_app=app,
                        numthreads=100)
    try:
        print("Serving on {}".format(server))
        server.start()
    except KeyboardInterrupt:
        server.stop()
示例#32
0
class WebServer:
  bind_hostname: str = None
  bind_port: str = None
  wsgi_applications: List[WebApp] = None
  server: WSGIServer = None

  @inject
  def __init__(self, bind_hostname: _WebserverBindHostnameKey,
               bind_port: _WebserverBindPortKey,
               wsgi_applications: List[WebApp]):
    self.bind_hostname = bind_hostname
    self.bind_port = bind_port
    self.wsgi_applications = wsgi_applications

    self.url_map = Map(
        [rule for app in self.wsgi_applications for rule in app.get_rules()])
    self.mount_map = {
        mount.mount_path: mount.app for app in self.wsgi_applications
        for mount in app.get_mounts()
    }

  def _handler(self, environ, start_response):
    adapter = self.url_map.bind_to_environ(environ)
    try:
      rule, values = adapter.match(return_rule=True)
    except HTTPException as e:
      return e.get_response(environ)(environ, start_response)

    def custom_start_response(status_, response_headers_, exc_info_=None):
      framework_webserver_endpoint_requests_z.labels(
          rule=rule.rule,
          method=environ['REQUEST_METHOD'],
          status=_get_status_code(status_)).inc()
      return start_response(status_, response_headers_, exc_info_)

    try:
      return rule.endpoint(environ, custom_start_response, **values)
    except HTTPException as e:
      return e.get_response(environ)(environ, custom_start_response)
    except Exception as e:
      log.exception('Unknown error occurred processing request')
      return InternalServerError(
          description='Unknown error occurred',
          original_exception=e).get_response(environ)(environ,
                                                      custom_start_response)

  def _server_thread(self):
    log.info(f'Starting webserver on {self.bind_hostname}:{self.bind_port}')
    app = self._handler
    app = DispatcherMiddleware(app, self.mount_map)
    app = WSGILogger(app)
    self.server = WSGIServer((self.bind_hostname, self.bind_port), app)
    self.server.stats['Enabled'] = True
    self.server.start()

  @execute()
  def run(self):
    t = Thread(target=self._server_thread, name='Webserver')
    t.start()
    return t

  @on_shutdown()
  def on_shutdown(self):
    log.info('Shutting down webserver')
    if self.server:
      self.server.stop()