中间件¶
Starlette 直接提供了几个可用的中间件。
阅读有关它们的更多信息,请访问 FastAPI 中间件文档。
fastapi.middleware.cors.CORSMiddleware ¶
CORSMiddleware(
app,
allow_origins=(),
allow_methods=("GET"),
allow_headers=(),
allow_credentials=False,
allow_origin_regex=None,
expose_headers=(),
max_age=600,
)
参数 | 描述 |
---|---|
app
|
类型: |
allow_origins
|
类型: |
allow_methods
|
类型: |
allow_headers
|
类型: |
allow_credentials
|
类型: |
allow_origin_regex
|
类型: |
expose_headers
|
类型: |
max_age
|
类型: |
starlette/middleware/cors.py
中的源代码
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
preflight_explicit_allow_origin 实例属性
¶
preflight_explicit_allow_origin = (
preflight_explicit_allow_origin
)
is_allowed_origin ¶
is_allowed_origin(origin)
参数 | 描述 |
---|---|
origin
|
类型: |
starlette/middleware/cors.py
中的源代码
95 96 97 98 99 100 101 102 |
|
preflight_response ¶
preflight_response(request_headers)
参数 | 描述 |
---|---|
request_headers
|
类型: |
starlette/middleware/cors.py
中的源代码
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
simple_response 异步
¶
simple_response(scope, receive, send, request_headers)
参数 | 描述 |
---|---|
scope
|
类型: |
receive
|
类型: |
send
|
类型: |
request_headers
|
类型: |
starlette/middleware/cors.py
中的源代码
142 143 144 |
|
send 异步
¶
send(message, send, request_headers)
参数 | 描述 |
---|---|
message
|
类型: |
send
|
类型: |
request_headers
|
类型: |
starlette/middleware/cors.py
中的源代码
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
allow_explicit_origin 静态方法
¶
allow_explicit_origin(headers, origin)
参数 | 描述 |
---|---|
headers
|
类型: |
origin
|
类型: |
starlette/middleware/cors.py
中的源代码
169 170 171 172 |
|
可以从fastapi
导入
from fastapi.middleware.cors import CORSMiddleware
fastapi.middleware.gzip.GZipMiddleware ¶
GZipMiddleware(app, minimum_size=500, compresslevel=9)
参数 | 描述 |
---|---|
app
|
类型: |
minimum_size
|
类型: |
compresslevel
|
类型: |
源代码在starlette/middleware/gzip.py
中
10 11 12 13 |
|
可以从fastapi
导入
from fastapi.middleware.gzip import GZipMiddleware
fastapi.middleware.httpsredirect.HTTPSRedirectMiddleware ¶
HTTPSRedirectMiddleware(app)
可以从fastapi
导入
from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware
fastapi.middleware.trustedhost.TrustedHostMiddleware ¶
TrustedHostMiddleware(
app, allowed_hosts=None, www_redirect=True
)
参数 | 描述 |
---|---|
app
|
类型: |
allowed_hosts
|
类型: |
www_redirect
|
类型: |
源代码在starlette/middleware/trustedhost.py
中
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
可以从fastapi
导入
from fastapi.middleware.trustedhost import TrustedHostMiddleware
fastapi.middleware.wsgi.WSGIMiddleware ¶
WSGIMiddleware(app)
可以从fastapi
导入
from fastapi.middleware.wsgi import WSGIMiddleware