提供跨源资源共享、访问控制、支持的quart扩展。
Quart-CORS的Python项目详细描述
quart cors是Quart的扩展,用于启用和控制Cross Origin Resource Sharing,cors 称为访问控制)。
由于Same Origin Policy,cors需要在浏览器中共享资源 防止资源从不同来源被使用。起源 在这种情况下,定义为方案、主机和端口的组合以及 资源对应于路径。
实际上,同源策略意味着浏览器访问 http://quart.com将阻止读取GET http://api.com的响应。它还将阻止诸如 POST http://api.com。注意,cors适用于浏览器启动的 请求、非浏览器客户端(如requests)不受 CORS限制。
cors允许服务器向浏览器指示某些资源 可以使用,与同源策略相反。它是通过 通知浏览器资源的访问控制头 使用。对于get请求,这些头将在响应中发送。为了 非get请求浏览器必须向服务器请求 在发送实际请求之前,它会这样做 通过飞行前选项请求。
同源策略不适用于WebSocket,因此 不需要穿紧身衣。相反,只有服务器负责 决定是否允许websocket,它应该通过检查 WebSocket请求源头。
简单(get)请求应返回cors头,指定 允许使用资源(响应)的源。这可能是 任何原点,*(通配符)或特定原点列表。这个 响应还应该包含一个cors头,指定 可以使用响应凭据,例如cookies。注意,如果凭证 允许共享允许的源必须是特定的而不是 通配符。
飞行前请求应返回指定原点的CORS头 允许使用资源,允许 向资源发送请求,是否可以 已使用,最后是可以使用的响应头。
请注意,在同一来源策略中允许某些操作,例如 例如<img src="http://api.com/img.gif">和simple 帖子。在本自述文件中,尽管这些复杂情况 忽略。
CORS访问控制响应头是,
Header name | Meaning |
Access-Control-Allow-Origin | Origins that are allowed to use the resource. |
Access-Control-Allow-Credentials | Can credentials be shared. |
Access-Control-Allow-Methods | Methods that may be used in requests to the resource. |
Access-Control-Allow-Headers | Headers that may be sent in requests to the resource. |
Access-Control-Expose-Headers | Headers that may be read in the response from the resource. |
Access-Control-Max-Age | Maximum age to cache the CORS headers for the resource. |
quart cors使用相同的命名(没有访问控制前缀) 因为当它们与同一个意义相关时,它是参数和设置。
用法
将cors访问控制头添加到 应用程序,只需对应用程序应用cors函数,或者 对于特定的蓝图,
app=Quart(__name__)app=cors(app,**settings)blueprint=Blueprint(__name__)blueprint=cors(blueprint,**settings)
或者,如果您希望按资源选择性地添加cors,请应用 路由的route_cors函数,或websocket_cors 函数指向WebSocket,
@app.route('/')@route_cors(**settings)asyncdefhandler():...@app.websocket('/')@websocket_cors(allow_origin=...)asyncdefhandler():...
settings是这些参数,
Argument | type |
allow_origin | Union[Set[str], str] |
allow_credentials | bool |
allow_methods | Union[Set[str], str] |
allow_headers | Union[Set[str], str] |
expose_headers | Union[Set[str], str] |
max_age | Union[int, flot, timedelta] |
与上面提到的CORS标题相对应。请注意 设置是可选的,可以在应用程序中指定默认值 配置,
Configuration key | type |
QUART_CORS_ALLOW_ORIGIN | Set[str] |
QUART_CORS_ALLOW_CREDENTIALS | bool |
QUART_CORS_ALLOW_METHODS | Set[str] |
QUART_CORS_ALLOW_HEADERS | Set[str] |
QUART_CORS_EXPOSE_HEADERS | Set[str] |
QUART_CORS_MAX_AGE | float |
websocket_corsdecorator只接受allow_origin 定义允许使用 韦博塞特。来自不允许来源的WebSocket请求将是 回复400。
简单示例
允许从任何来源使用应用程序(不推荐,因为它是 太宽容了),
app=Quart(__name__)app=cors(app,allow_origin="*")
要允许从另一个特定域使用路由或WebSocket, https://quart.com,
@app.route('/')@route_cors(allow_origin="https://quart.com")asyncdefhandler():...@app.websocket('/')@websocket_cors(allow_origin="https://quart.com")asyncdefhandler():...
允许json post请求到api路由,从https://quart.com,
@app.route('/',methods=["POST"])@route_cors(allow_headers=["content-type"],allow_methods=["POST"],allow_origin=["https://quart.com"],)asyncdefhandler():data=awaitrequest.get_json()...
贡献
夸脱cors是在GitLab上开发的。很欢迎你来 打开issues或 提议merge requests。
测试
测试夸脱cors的最佳方法是使用tox,
$ pip install tox $ tox
这将检查代码样式并运行测试。
帮助
这个自述文件是最好的开始,之后尝试打开 issue。