本文整理汇总了Python中aiohttp.hdrs.METH_OPTIONS属性的典型用法代码示例。如果您正苦于以下问题:Python hdrs.METH_OPTIONS属性的具体用法?Python hdrs.METH_OPTIONS怎么用?Python hdrs.METH_OPTIONS使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类aiohttp.hdrs
的用法示例。
在下文中一共展示了hdrs.METH_OPTIONS属性的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process
# 需要导入模块: from aiohttp import hdrs [as 别名]
# 或者: from aiohttp.hdrs import METH_OPTIONS [as 别名]
def process(self):
request = self.request
if request.method == hdrs.METH_OPTIONS:
headers = (
(hdrs.CONTENT_TYPE, "application/javascript; charset=UTF-8"),
(hdrs.ACCESS_CONTROL_ALLOW_METHODS, "OPTIONS, POST"),
)
headers += session_cookie(request)
headers += cors_headers(request.headers)
headers += cache_headers()
return web.Response(status=204, headers=headers)
headers = (
(hdrs.CONTENT_TYPE, "application/javascript; charset=UTF-8"),
(hdrs.CACHE_CONTROL, CACHE_CONTROL),
)
headers += session_cookie(request)
headers += cors_headers(request.headers)
resp = self.response = web.StreamResponse(headers=headers)
await resp.prepare(request)
await self.handle_session()
return resp
示例2: options
# 需要导入模块: from aiohttp import hdrs [as 别名]
# 或者: from aiohttp.hdrs import METH_OPTIONS [as 别名]
def options(path: str, **kwargs: Any) -> WebHandlerDecorator:
return handle(hdrs.METH_OPTIONS, path, **kwargs)
示例3: process
# 需要导入模块: from aiohttp import hdrs [as 别名]
# 或者: from aiohttp.hdrs import METH_OPTIONS [as 别名]
def process(self):
request = self.request
if request.method not in (hdrs.METH_GET, hdrs.METH_POST, hdrs.METH_OPTIONS):
return web.HTTPForbidden(text="Method is not allowed")
if self.request.method == hdrs.METH_OPTIONS:
headers = (
(hdrs.ACCESS_CONTROL_ALLOW_METHODS, "OPTIONS, POST"),
(hdrs.CONTENT_TYPE, "application/javascript; charset=UTF-8"),
)
headers += session_cookie(request)
headers += cors_headers(request.headers)
headers += cache_headers()
return web.Response(status=204, headers=headers)
data = await request.read()
if not data:
return web.HTTPInternalServerError(text="Payload expected.")
try:
messages = loads(data.decode(ENCODING))
except Exception:
return web.HTTPInternalServerError(text="Broken JSON encoding.")
await self.session._remote_messages(messages)
headers = (
(hdrs.CONTENT_TYPE, "text/plain; charset=UTF-8"),
(hdrs.CACHE_CONTROL, CACHE_CONTROL),
)
headers += session_cookie(request)
headers += cors_headers(request.headers)
return web.Response(status=204, headers=headers)
示例4: process
# 需要导入模块: from aiohttp import hdrs [as 别名]
# 或者: from aiohttp.hdrs import METH_OPTIONS [as 别名]
def process(self):
request = self.request
headers = (
(hdrs.CONNECTION, request.headers.get(hdrs.CONNECTION, "close")),
(hdrs.CONTENT_TYPE, "application/javascript; charset=UTF-8"),
(hdrs.CACHE_CONTROL, CACHE_CONTROL),
)
headers += session_cookie(request)
headers += cors_headers(request.headers)
if request.method == hdrs.METH_OPTIONS:
headers += ((hdrs.ACCESS_CONTROL_ALLOW_METHODS, "OPTIONS, POST"),)
headers += cache_headers()
return web.Response(status=204, headers=headers)
# open sequence (sockjs protocol)
resp = self.response = web.StreamResponse(headers=headers)
resp.force_close()
await resp.prepare(request)
await resp.write(self.open_seq)
# event loop
await self.handle_session()
return resp
示例5: set_cors
# 需要导入模块: from aiohttp import hdrs [as 别名]
# 或者: from aiohttp.hdrs import METH_OPTIONS [as 别名]
def set_cors(self, app, *, domains='*', handler=None, headers=()):
assert app.router is self, 'Application must be initialized ' \
'with this instance router'
self._domains = domains
self._cors_headers = headers
if self._default_options_route is None:
app.on_response_prepare.append(self.cors_on_prepare)
if handler is None:
handler = self.cors_options
if handler is not None:
self._default_options_route = Route(
hdrs.METH_OPTIONS, handler, self._resource)
示例6: add_options
# 需要导入模块: from aiohttp import hdrs [as 别名]
# 或者: from aiohttp.hdrs import METH_OPTIONS [as 别名]
def add_options(
self, path: str, handler: WebHandler, **kwargs: Any
) -> web.AbstractRoute:
return self.add_route(hdrs.METH_OPTIONS, path, handler, **kwargs)
示例7: test_all_methods
# 需要导入模块: from aiohttp import hdrs [as 别名]
# 或者: from aiohttp.hdrs import METH_OPTIONS [as 别名]
def test_all_methods(swagger_docs, aiohttp_client):
class View(web.View):
async def get(self):
"""
---
responses:
'200':
description: OK.
"""
return web.json_response()
async def handler(request):
"""
---
responses:
'200':
description: OK.
"""
return web.json_response()
swagger = swagger_docs()
swagger.add_get("/r", handler, allow_head=False),
swagger.add_head("/r", handler),
swagger.add_put("/r", handler),
swagger.add_patch("/r", handler),
swagger.add_post("/r", handler),
swagger.add_delete("/r", handler),
swagger.add_options("/r", handler),
swagger.add_view("/r2", View),
client = await aiohttp_client(swagger._app)
for method in (
hdrs.METH_GET,
hdrs.METH_HEAD,
hdrs.METH_POST,
hdrs.METH_PUT,
hdrs.METH_PATCH,
hdrs.METH_DELETE,
hdrs.METH_OPTIONS,
):
resp = await getattr(client, method.lower())("/r")
assert resp.status == 200
resp = await client.get("/r2")
assert resp.status == 200