當前位置: 首頁>>代碼示例>>Python>>正文


Python cors.CORSMiddleware方法代碼示例

本文整理匯總了Python中starlette.middleware.cors.CORSMiddleware方法的典型用法代碼示例。如果您正苦於以下問題:Python cors.CORSMiddleware方法的具體用法?Python cors.CORSMiddleware怎麽用?Python cors.CORSMiddleware使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在starlette.middleware.cors的用法示例。


在下文中一共展示了cors.CORSMiddleware方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: use_cors

# 需要導入模塊: from starlette.middleware import cors [as 別名]
# 或者: from starlette.middleware.cors import CORSMiddleware [as 別名]
def use_cors(app: "App"):
    """Enable CORS (Cross-Origin Resource Sharing) headers.

    [constants.py]: /api/constants.md
    [CORSMiddleware]: https://www.starlette.io/middleware/#corsmiddleware

    Settings:
    - `CORS` (bool or dict):
        if `True`, the default configuration defined in [constants.py] is used.
        Otherwise, the dictionary is passed to Starlette's [CORSMiddleware].
    """
    cors: typing.Optional[typing.Union[bool, dict]] = settings.get("CORS")

    if cors is None:
        return

    if cors is True:
        cors = dict(DEFAULT_CORS_CONFIG)

    app.add_middleware(CORSMiddleware, **cors) 
開發者ID:bocadilloproject,項目名稱:bocadillo,代碼行數:22,代碼來源:plugins.py

示例2: __init__

# 需要導入模塊: from starlette.middleware import cors [as 別名]
# 或者: from starlette.middleware.cors import CORSMiddleware [as 別名]
def __init__(self, units, **kwargs):
        if not isinstance(units, list):
            raise BootstrapError(
                f"aioli.Application expects an iterable of Units, got: {type(units)}"
            )

        config = kwargs.pop("config", {})
        self.__units = units

        try:
            self.config = ApplicationConfigSchema().load(config.get("aioli-core", {}))
        except ValueError:
            raise BootstrapError("Application `config` must be a collection")
        except ValidationError as e:
            raise BootstrapError(f"Configuration validation error: {e.messages}")

        self.registry = ImportRegistry(self, config)

        # Apply known settings from environment or provided `config`
        super(Application, self).__init__(debug=self.config["debug"], **kwargs)

        # Error handlers
        self.add_exception_handler(AioliException, http_error)
        self.add_exception_handler(HTTPException, http_error)
        self.add_exception_handler(ValidationError, validation_error)
        self.add_exception_handler(JSONDecodeError, decode_error)

        # Middleware
        self.add_middleware(CORSMiddleware, allow_origins=self.config["allow_origins"])

        # Lifespan handlers
        self.router.lifespan.add_event_handler("startup", self._startup)
        self.router.lifespan.add_event_handler("shutdown", self._shutdown) 
開發者ID:aioli-framework,項目名稱:aioli,代碼行數:35,代碼來源:app.py


注:本文中的starlette.middleware.cors.CORSMiddleware方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。