async def test_with_blueprint_absolute(self, app, mocker): bp = Blueprint('foo', __name__, url_prefix='/foo') bp.add_url_rule('/<foo>', 'foobar', view_func=lambda x: x) app.register_blueprint(bp) field = fields.Url(absolute=True) obj = mocker.Mock(foo=42) async with app.test_request_context('/foo/foo'): assert 'http://localhost/foo/42' == field.output('foo', obj)
async def test_with_blueprint_invalid_object(self, app): bp = Blueprint('foo', __name__, url_prefix='/foo') bp.add_url_rule('/<foo>', 'foobar', view_func=lambda x: x) app.register_blueprint(bp) field = fields.Url() async with app.test_request_context('/foo/foo'): with pytest.raises(fields.MarshallingError): field.output('foo', None)
async def test_blueprint_method_view(app: Pint) -> None: blueprint = Blueprint('blueprint', __name__) class Views(MethodView): async def get(self) -> ResponseReturnValue: return 'GET' async def post(self) -> ResponseReturnValue: return 'POST' blueprint.add_url_rule('/', view_func=Views.as_view('simple')) app.register_blueprint(blueprint) test_client = app.test_client() response = await test_client.get('/') assert 'GET' == (await response.get_data(raw=False)) response = await test_client.post('/') assert 'POST' == (await response.get_data(raw=False))
async def test_blueprint_method_view() -> None: app = Quart(__name__) blueprint = Blueprint("blueprint", __name__) class Views(MethodView): async def get(self) -> ResponseReturnValue: return "GET" async def post(self) -> ResponseReturnValue: return "POST" blueprint.add_url_rule("/", view_func=Views.as_view("simple")) app.register_blueprint(blueprint) test_client = app.test_client() response = await test_client.get("/") assert "GET" == (await response.get_data(as_text=True)) response = await test_client.post("/") assert "POST" == (await response.get_data(as_text=True))
def run(self): """ While we could use the standard decorators to register these routes, using add_url_rule() allows us to create diffrent endpoint names programmatically and pass the classes self object to the routes """ config = Config() config.accesslog = os.path.join(get_path_in_data_folder("logs"), "access.log") config.bind = f"{self['BindIP']}:{self['Port']}" config.insecure_bind = True config.include_server_header = False config.use_reloader = False config.debug = False http_blueprint = Blueprint(__name__, 'http') http_blueprint.before_request(self.check_if_naughty) #http_blueprint.after_request(self.make_normal) http_blueprint.add_url_rule('/<uuid:GUID>', 'key_exchange', self.key_exchange, methods=['POST']) http_blueprint.add_url_rule('/<uuid:GUID>', 'stage', self.stage, methods=['GET']) http_blueprint.add_url_rule('/<uuid:GUID>/jobs', 'jobs', self.jobs, methods=['GET']) http_blueprint.add_url_rule('/<uuid:GUID>/jobs/<job_id>', 'job_result', self.job_result, methods=['POST']) # Add a catch all route http_blueprint.add_url_rule('/', 'unknown_path', self.unknown_path, defaults={'path': ''}) http_blueprint.add_url_rule('/<path:path>', 'unknown_path', self.unknown_path, methods=['GET', 'POST']) #logging.getLogger('quart.app').setLevel(logging.DEBUG if state.args['--debug'] else logging.ERROR) #logging.getLogger('quart.serving').setLevel(logging.DEBUG if state.args['--debug'] else logging.ERROR) self.app = Quart(__name__) self.app.register_blueprint(http_blueprint) asyncio.run(serve(self.app, config))
def run(self): ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) ssl_context.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 | ssl.OP_NO_COMPRESSION ssl_context.set_ciphers('ECDHE+AESGCM') ssl_context.load_cert_chain(certfile=self['Cert'], keyfile=self['Key']) ssl_context.set_alpn_protocols(['http/1.1']) # Only http/1.1 """ While we could use the standard decorators to register these routes, using add_url_rule() allows us to create diffrent endpoint names programmatically and pass the classes self object to the routes """ loop = asyncio.get_event_loop() http_blueprint = Blueprint(__name__, 'http') http_blueprint.before_request(self.check_if_naughty) http_blueprint.after_request(self.make_normal) http_blueprint.add_url_rule('/stage.zip', 'stage', self.stage, methods=['GET']) http_blueprint.add_url_rule('/<GUID>', 'first_checkin', self.first_checkin, methods=['POST']) http_blueprint.add_url_rule('/<GUID>/jobs', 'jobs', self.jobs, methods=['GET']) http_blueprint.add_url_rule('/<GUID>/jobs/<job_id>', 'job_result', self.job_result, methods=['POST']) # Add a catch all route http_blueprint.add_url_rule('/', 'unknown_path', self.unknown_path, defaults={'path': ''}) http_blueprint.add_url_rule('/<path:path>', 'unknown_path', self.unknown_path, methods=['GET', 'POST']) self.app = Quart(__name__) logging.getLogger('quart.app').setLevel( logging.DEBUG if state.args['--debug'] else logging.ERROR) logging.getLogger('quart.serving').setLevel( logging.DEBUG if state.args['--debug'] else logging.ERROR) self.app.register_blueprint(http_blueprint) self.app.run( host=self['BindIP'], port=self['Port'], debug=False, ssl=ssl_context, use_reloader=False, access_log_format= '%(h)s %(p)s - - %(t)s statusline: "%(r)s" statuscode: %(s)s responselen: %(b)s protocol: %(H)s', loop=loop)
from quart import Blueprint from . import views bp = Blueprint("root", __name__) bp.add_url_rule("/", view_func=views.home) # for ease of development -- will be aggregated for production server def static(static_type, mimetype=None): import utils, glob, os from quart import send_file async def func(file_path): for x in glob.glob(utils.WEBVIEW_DIR + f"*/{static_type}/*.{static_type}"): if os.path.basename(x).lower() == os.path.basename( file_path).lower(): return await send_file(x, mimetype=mimetype) return "" return func bp.add_url_rule("/js/<path:file_path>/", endpoint="js", view_func=static('js', mimetype="application/javascript")) bp.add_url_rule("/css/<path:file_path>/", endpoint="css", view_func=static('css'))
def run(self): """ While we could use the standard decorators to register these routes, using add_url_rule() allows us to create diffrent endpoint names programmatically and pass the classes self object to the routes """ loop = asyncio.get_event_loop() http_blueprint = Blueprint(__name__, 'http') http_blueprint.before_request(self.check_if_naughty) #http_blueprint.after_request(self.make_normal) http_blueprint.add_url_rule('/<uuid:GUID>', 'key_exchange', self.key_exchange, methods=['POST']) http_blueprint.add_url_rule('/<uuid:GUID>', 'stage', self.stage, methods=['GET']) http_blueprint.add_url_rule('/<uuid:GUID>/jobs', 'jobs', self.jobs, methods=['GET']) http_blueprint.add_url_rule('/<uuid:GUID>/jobs/<job_id>', 'job_result', self.job_result, methods=['POST']) # Add a catch all route http_blueprint.add_url_rule('/', 'unknown_path', self.unknown_path, defaults={'path': ''}) http_blueprint.add_url_rule('/<path:path>', 'unknown_path', self.unknown_path, methods=['GET', 'POST']) self.app = Quart(__name__) logging.getLogger('quart.app').setLevel(logging.DEBUG if state.args['--debug'] else logging.ERROR) logging.getLogger('quart.serving').setLevel(logging.DEBUG if state.args['--debug'] else logging.ERROR) #serving_handler.setFormatter('%(h)s %(p)s - - %(t)s statusline: "%(r)s" statuscode: %(s)s responselen: %(b)s protocol: %(H)s') #logging.getLogger('quart.app').removeHandler(default_handler) self.app.register_blueprint(http_blueprint) self.app.run(host=self['BindIP'], port=self['Port'], debug=False, use_reloader=False, #access_log_format=, loop=loop)
def run(self): #ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) #ssl_context.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 | ssl.OP_NO_COMPRESSION #ssl_context.set_ciphers('ECDHE+AESGCM') #ssl_context.load_cert_chain(, ) #ssl_context.set_alpn_protocols(['http/1.1', 'h2']) if (self['Key'] == 'data/key.pem') and (self['Cert'] == 'data/cert.pem'): if not os.path.exists(self['Key']) or not os.path.exists( self['Cert']) or self['RegenCert']: create_self_signed_cert() """ While we could use the standard decorators to register these routes, using add_url_rule() allows us to create diffrent endpoint names programmatically and pass the classes self object to the routes """ loop = asyncio.get_event_loop() http_blueprint = Blueprint(__name__, 'https') http_blueprint.before_request(self.check_if_naughty) #http_blueprint.after_request(self.make_normal) http_blueprint.add_url_rule('/<uuid:GUID>', 'key_exchange', self.key_exchange, methods=['POST']) http_blueprint.add_url_rule('/<uuid:GUID>', 'stage', self.stage, methods=['GET']) http_blueprint.add_url_rule('/<uuid:GUID>/jobs', 'jobs', self.jobs, methods=['GET']) http_blueprint.add_url_rule('/<uuid:GUID>/jobs/<job_id>', 'job_result', self.job_result, methods=['POST']) # Add a catch all route http_blueprint.add_url_rule('/', 'unknown_path', self.unknown_path, defaults={'path': ''}) http_blueprint.add_url_rule('/<path:path>', 'unknown_path', self.unknown_path, methods=['GET', 'POST']) self.app = Quart(__name__) logging.getLogger('quart.app').setLevel( logging.DEBUG if state.args['--debug'] else logging.ERROR) logging.getLogger('quart.serving').setLevel( logging.DEBUG if state.args['--debug'] else logging.ERROR) #serving_handler.setFormatter('%(h)s %(p)s - - %(t)s statusline: "%(r)s" statuscode: %(s)s responselen: %(b)s protocol: %(H)s') #logging.getLogger('quart.app').removeHandler(default_handler) self.app.register_blueprint(http_blueprint) self.app.run( host=self['BindIP'], port=self['Port'], debug=False, #ssl=ssl_context, certfile=self['Cert'], keyfile=self['Key'], use_reloader=False, #access_log_format=, loop=loop)
def run(self): if (self['Key'] == '~/.st/key.pem') and (self['Cert'] == '~/.st/cert.pem'): if not os.path.exists( get_path_in_data_folder("key.pem")) or not os.path.exists( get_path_in_data_folder( "cert.pem")) or self['RegenCert']: create_self_signed_cert() config = Config() config.ciphers = 'ALL' config.accesslog = os.path.join(get_path_in_data_folder("logs"), "access.log") config.bind = f"{self['BindIP']}:{self['Port']}" config.certfile = os.path.expanduser(self['Cert']) config.keyfile = os.path.expanduser(self['Key']) config.include_server_header = False # This doesn't seem to do anything? config.use_reloader = False config.debug = False """ While we could use the standard decorators to register these routes, using add_url_rule() allows us to create diffrent endpoint names programmatically and pass the classes self object to the routes """ http_blueprint = Blueprint(__name__, 'https') http_blueprint.before_request(self.check_if_naughty) #http_blueprint.after_request(self.make_normal) http_blueprint.add_url_rule('/<uuid:GUID>', 'key_exchange', self.key_exchange, methods=['POST']) http_blueprint.add_url_rule('/<uuid:GUID>', 'stage', self.stage, methods=['GET']) http_blueprint.add_url_rule('/<uuid:GUID>/jobs', 'jobs', self.jobs, methods=['GET']) http_blueprint.add_url_rule('/<uuid:GUID>/jobs/<job_id>', 'job_result', self.job_result, methods=['POST']) # Add a catch all route http_blueprint.add_url_rule('/', 'unknown_path', self.unknown_path, defaults={'path': ''}) http_blueprint.add_url_rule('/<path:path>', 'unknown_path', self.unknown_path, methods=['GET', 'POST']) #logging.getLogger('quart.app').setLevel(logging.DEBUG if state.args['--debug'] else logging.ERROR) #logging.getLogger('quart.serving').setLevel(logging.DEBUG if state.args['--debug'] else logging.ERROR) self.app = Quart(__name__) self.app.register_blueprint(http_blueprint) asyncio.run(serve(self.app, config))
def run(self): #ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) #ssl_context.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 | ssl.OP_NO_COMPRESSION #ssl_context.set_ciphers('ECDHE+AESGCM') #ssl_context.load_cert_chain(, ) #ssl_context.set_alpn_protocols(['http/1.1', 'h2']) if (self['Key'] == 'data/key.pem') and (self['Cert'] == 'data/cert.pem'): if not os.path.exists(self['Key']) or not os.path.exists(self['Cert']) or self['RegenCert']: create_self_signed_cert() """ While we could use the standard decorators to register these routes, using add_url_rule() allows us to create diffrent endpoint names programmatically and pass the classes self object to the routes """ loop = asyncio.get_event_loop() http_blueprint = Blueprint(__name__, 'https') http_blueprint.before_request(self.check_if_naughty) #http_blueprint.after_request(self.make_normal) http_blueprint.add_url_rule('/<uuid:GUID>', 'key_exchange', self.key_exchange, methods=['POST']) http_blueprint.add_url_rule('/<uuid:GUID>', 'stage', self.stage, methods=['GET']) http_blueprint.add_url_rule('/<uuid:GUID>/jobs', 'jobs', self.jobs, methods=['GET']) http_blueprint.add_url_rule('/<uuid:GUID>/jobs/<job_id>', 'job_result', self.job_result, methods=['POST']) # Add a catch all route http_blueprint.add_url_rule('/', 'unknown_path', self.unknown_path, defaults={'path': ''}) http_blueprint.add_url_rule('/<path:path>', 'unknown_path', self.unknown_path, methods=['GET', 'POST']) self.app = Quart(__name__) logging.getLogger('quart.app').setLevel(logging.DEBUG if state.args['--debug'] else logging.ERROR) logging.getLogger('quart.serving').setLevel(logging.DEBUG if state.args['--debug'] else logging.ERROR) #serving_handler.setFormatter('%(h)s %(p)s - - %(t)s statusline: "%(r)s" statuscode: %(s)s responselen: %(b)s protocol: %(H)s') #logging.getLogger('quart.app').removeHandler(default_handler) self.app.register_blueprint(http_blueprint) self.app.run(host=self['BindIP'], port=self['Port'], debug=False, #ssl=ssl_context, certfile=self['Cert'], keyfile=self['Key'], use_reloader=False, #access_log_format=, loop=loop)
def run(self): """ While we could use the standard decorators to register these routes, using add_url_rule() allows us to create diffrent endpoint names programmatically and pass the classes self object to the routes """ loop = asyncio.get_event_loop() http_blueprint = Blueprint(__name__, 'http') http_blueprint.before_request(self.check_if_naughty) #http_blueprint.after_request(self.make_normal) http_blueprint.add_url_rule('/<uuid:GUID>', 'key_exchange', self.key_exchange, methods=['POST']) http_blueprint.add_url_rule('/<uuid:GUID>', 'stage', self.stage, methods=['GET']) http_blueprint.add_url_rule('/<uuid:GUID>/jobs', 'jobs', self.jobs, methods=['GET']) http_blueprint.add_url_rule('/<uuid:GUID>/jobs/<job_id>', 'job_result', self.job_result, methods=['POST']) # Add a catch all route http_blueprint.add_url_rule('/', 'unknown_path', self.unknown_path, defaults={'path': ''}) http_blueprint.add_url_rule('/<path:path>', 'unknown_path', self.unknown_path, methods=['GET', 'POST']) self.app = Quart(__name__) logging.getLogger('quart.app').setLevel( logging.DEBUG if state.args['--debug'] else logging.ERROR) logging.getLogger('quart.serving').setLevel( logging.DEBUG if state.args['--debug'] else logging.ERROR) #serving_handler.setFormatter('%(h)s %(p)s - - %(t)s statusline: "%(r)s" statuscode: %(s)s responselen: %(b)s protocol: %(H)s') #logging.getLogger('quart.app').removeHandler(default_handler) self.app.register_blueprint(http_blueprint) self.app.run( host=self['BindIP'], port=self['Port'], debug=False, use_reloader=False, #access_log_format=, loop=loop)
) ''' ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) ssl_context.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 | ssl.OP_NO_COMPRESSION ssl_context.set_ciphers('ECHDE+AESGCM:ECDH+AES') ssl_context.load_cert_chain(certfile='./data/cert.pem', keyfile='./data/key.pem') ssl_context.set_alpn_protocols(['http/1.1', 'h2']) # accept http1 and http2 connections ''' #loop = asyncio.get_event_loop() http_blueprint = Blueprint(__name__, 'http') #http_blueprint.before_request(check_if_naughty) #http_blueprint.after_request(make_normal) http_blueprint.add_url_rule('/stage.zip', 'stage', stage, methods=['GET']) http_blueprint.add_url_rule('/job.zip', 'job', job, methods=['GET']) http_blueprint.add_url_rule('/job', 'job_result', job_result, methods=['POST']) # Add a catch all route http_blueprint.add_url_rule('/', 'unknown_path', unknown_path, defaults={'path': ''}) http_blueprint.add_url_rule('/<path:path>', 'unknown_path', unknown_path, methods=['GET', 'POST'])
def run(self): if (self['Key'] == f'{self.certs_path}/artic2_private.key') and ( self['Cert'] == f'{self.certs_path}/artic2_cert.pem'): if not os.path.exists(get_path_in_data_folder( "artic2_private.key")) or not os.path.exists( get_path_in_data_folder( "artic2_cert.pem")) or self['RegenCert']: create_self_signed_cert() config = Config() config.ciphers = 'ALL' config.accesslog = os.path.join(get_path_in_artic2("logs"), "access.log") config.bind = f"{self['BindIP']}:{self['Port']}" config.certfile = os.path.expanduser(self['Cert']) config.keyfile = os.path.expanduser(self['Key']) config.include_server_header = False config.use_reloader = False config.debug = False """ While we could use the standard decorators to register these routes, using add_url_rule() allows us to create diffrent endpoint names programmatically and pass the classes self object to the routes """ http_blueprint = Blueprint(__name__, 'https') http_blueprint.before_request(self.check_if_naughty) http_blueprint.add_url_rule('/<uuid:GUID>', 'key_exchange', self.key_exchange, methods=['POST']) http_blueprint.add_url_rule('/<uuid:GUID>', 'stage', self.stage, methods=['GET']) http_blueprint.add_url_rule('/<uuid:GUID>/jobs', 'jobs', self.jobs, methods=['GET']) http_blueprint.add_url_rule('/<uuid:GUID>/jobs/<job_id>', 'job_result', self.job_result, methods=['POST']) http_blueprint.add_url_rule('/', 'unknown_path', self.unknown_path, defaults={'path': ''}) http_blueprint.add_url_rule('/<path:path>', 'unknown_path', self.unknown_path, methods=['GET', 'POST']) self.app = Quart(__name__) self.app.register_blueprint(http_blueprint) asyncio.run(serve(self.app, config))
from quart import Blueprint from . import views bp = Blueprint("settings", __name__) bp.add_url_rule("/settings/", view_func=views.SettingsView.as_view("index")) bp.add_url_rule("/settings/roles", view_func=views.RoleView.as_view("role_post"))