本文整理匯總了Python中aiohttp_cors.ResourceOptions方法的典型用法代碼示例。如果您正苦於以下問題:Python aiohttp_cors.ResourceOptions方法的具體用法?Python aiohttp_cors.ResourceOptions怎麽用?Python aiohttp_cors.ResourceOptions使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類aiohttp_cors
的用法示例。
在下文中一共展示了aiohttp_cors.ResourceOptions方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: start
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def start(self):
app = web.Application(client_max_size=JSON_RPC_CLIENT_REQUEST_MAX_SIZE)
cors = aiohttp_cors.setup(app)
route = app.router.add_post("/", self.__handle)
cors.add(
route,
{
"*": aiohttp_cors.ResourceOptions(
allow_credentials=True,
expose_headers=("X-Custom-Server-Header",),
allow_methods=["POST", "PUT"],
allow_headers=("X-Requested-With", "Content-Type"),
)
},
)
self.runner = web.AppRunner(app, access_log=None)
self.loop.run_until_complete(self.runner.setup())
site = web.TCPSite(self.runner, self.host, self.port)
self.loop.run_until_complete(site.start())
示例2: add_routes
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def add_routes(app, with_ui=False):
{# URLs are traverse in reversed sorted order so that longer ones are evaluated first #}
{% for relative_url, class_name in entries|dictsort(true)|reverse %}
app.router.add_view(r"/{{ relative_url }}", views.{{ class_name }})
{% endfor %}
if with_ui:
app.router.add_view(r"/the_specification", views.__SWAGGER_SPEC__)
app.router.add_static(r"/ui", path="ui")
# Configure default CORS settings.
cors = aiohttp_cors.setup(app, defaults={
"*": aiohttp_cors.ResourceOptions(
allow_credentials=True,
expose_headers="*",
allow_headers="*",
)
})
# Configure CORS on all routes.
for route in app.router.routes():
cors.add(route)
示例3: test_simple_no_origin
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def test_simple_no_origin(aiohttp_client, make_app):
app = make_app(None, {"http://client1.example.org":
ResourceOptions()})
client = await aiohttp_client(app)
resp = await client.get("/resource")
assert resp.status == 200
resp_text = await resp.text()
assert resp_text == TEST_BODY
for header_name in {
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN,
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS,
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS,
}:
assert header_name not in resp.headers
示例4: test_simple_allowed_origin
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def test_simple_allowed_origin(aiohttp_client, make_app):
app = make_app(None, {"http://client1.example.org":
ResourceOptions()})
client = await aiohttp_client(app)
resp = await client.get("/resource",
headers={hdrs.ORIGIN:
'http://client1.example.org'})
assert resp.status == 200
resp_text = await resp.text()
assert resp_text == TEST_BODY
for hdr, val in {
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN: 'http://client1.example.org',
}.items():
assert resp.headers.get(hdr) == val
for header_name in {
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS,
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS,
}:
assert header_name not in resp.headers
示例5: test_simple_not_allowed_origin
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def test_simple_not_allowed_origin(aiohttp_client, make_app):
app = make_app(None, {"http://client1.example.org":
ResourceOptions()})
client = await aiohttp_client(app)
resp = await client.get("/resource",
headers={hdrs.ORIGIN:
'http://client2.example.org'})
assert resp.status == 200
resp_text = await resp.text()
assert resp_text == TEST_BODY
for header_name in {
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN,
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS,
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS,
}:
assert header_name not in resp.headers
示例6: test_simple_explicit_port
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def test_simple_explicit_port(aiohttp_client, make_app):
app = make_app(None, {"http://client1.example.org":
ResourceOptions()})
client = await aiohttp_client(app)
resp = await client.get("/resource",
headers={hdrs.ORIGIN:
'http://client1.example.org:80'})
assert resp.status == 200
resp_text = await resp.text()
assert resp_text == TEST_BODY
for header_name in {
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN,
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS,
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS,
}:
assert header_name not in resp.headers
示例7: test_simple_different_scheme
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def test_simple_different_scheme(aiohttp_client, make_app):
app = make_app(None, {"http://client1.example.org":
ResourceOptions()})
client = await aiohttp_client(app)
resp = await client.get("/resource",
headers={hdrs.ORIGIN:
'https://client1.example.org'})
assert resp.status == 200
resp_text = await resp.text()
assert resp_text == TEST_BODY
for header_name in {
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN,
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS,
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS,
}:
assert header_name not in resp.headers
示例8: test_simple_expose_headers_allowed_origin
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def test_simple_expose_headers_allowed_origin(aiohttp_client, make_app):
app = make_app(None, {"http://client1.example.org":
ResourceOptions(
expose_headers=(SERVER_CUSTOM_HEADER_NAME,))})
client = await aiohttp_client(app)
resp = await client.get("/resource",
headers={hdrs.ORIGIN:
'http://client1.example.org'})
assert resp.status == 200
resp_text = await resp.text()
assert resp_text == TEST_BODY
for hdr, val in {
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN: 'http://client1.example.org',
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS:
SERVER_CUSTOM_HEADER_NAME}.items():
assert resp.headers.get(hdr) == val
for header_name in {
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS,
}:
assert header_name not in resp.headers
示例9: test_simple_expose_headers_not_allowed_origin
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def test_simple_expose_headers_not_allowed_origin(aiohttp_client,
make_app):
app = make_app(None, {"http://client1.example.org":
ResourceOptions(
expose_headers=(SERVER_CUSTOM_HEADER_NAME,))})
client = await aiohttp_client(app)
resp = await client.get("/resource",
headers={hdrs.ORIGIN:
'http://client2.example.org'})
assert resp.status == 200
resp_text = await resp.text()
assert resp_text == TEST_BODY
for header_name in {
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN,
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS,
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS,
}:
assert header_name not in resp.headers
示例10: test_preflight_default_no_origin
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def test_preflight_default_no_origin(aiohttp_client, make_app):
app = make_app(None, {"http://client1.example.org":
ResourceOptions()})
client = await aiohttp_client(app)
resp = await client.options("/resource")
assert resp.status == 403
resp_text = await resp.text()
assert "origin header is not specified" in resp_text
for header_name in {
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN,
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS,
hdrs.ACCESS_CONTROL_MAX_AGE,
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS,
hdrs.ACCESS_CONTROL_ALLOW_METHODS,
hdrs.ACCESS_CONTROL_ALLOW_HEADERS,
}:
assert header_name not in resp.headers
示例11: test_preflight_default_no_method
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def test_preflight_default_no_method(aiohttp_client, make_app):
app = make_app(None, {"http://client1.example.org":
ResourceOptions()})
client = await aiohttp_client(app)
resp = await client.options("/resource", headers={
hdrs.ORIGIN: "http://client1.example.org",
})
assert resp.status == 403
resp_text = await resp.text()
assert "'Access-Control-Request-Method' header is not specified"\
in resp_text
for header_name in {
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN,
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS,
hdrs.ACCESS_CONTROL_MAX_AGE,
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS,
hdrs.ACCESS_CONTROL_ALLOW_METHODS,
hdrs.ACCESS_CONTROL_ALLOW_HEADERS,
}:
assert header_name not in resp.headers
示例12: test_preflight_default_disallowed_method
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def test_preflight_default_disallowed_method(aiohttp_client, make_app):
app = make_app(None, {"http://client1.example.org":
ResourceOptions()})
client = await aiohttp_client(app)
resp = await client.options("/resource", headers={
hdrs.ORIGIN: "http://client1.example.org",
hdrs.ACCESS_CONTROL_REQUEST_METHOD: "POST",
})
assert resp.status == 403
resp_text = await resp.text()
assert ("request method 'POST' is not allowed for "
"'http://client1.example.org' origin" in resp_text)
for header_name in {
hdrs.ACCESS_CONTROL_ALLOW_ORIGIN,
hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS,
hdrs.ACCESS_CONTROL_MAX_AGE,
hdrs.ACCESS_CONTROL_EXPOSE_HEADERS,
hdrs.ACCESS_CONTROL_ALLOW_METHODS,
hdrs.ACCESS_CONTROL_ALLOW_HEADERS,
}:
assert header_name not in resp.headers
示例13: __init__
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def __init__(self, bot: Bot, host: str, port: int):
self.bot = bot
self.app = web.Application(middlewares=[web.normalize_path_middleware()])
self.app.add_routes([
web.post('/callback', self.callback)
])
cors = aiohttp_cors.setup(self.app, defaults={
"*": aiohttp_cors.ResourceOptions(
allow_credentials=True,
expose_headers="*",
allow_headers="*",
)
})
ufw_resource = cors.add(self.app.router.add_resource("/ufw/{wallet}"))
cors.add(ufw_resource.add_route("GET", self.ufw))
wfu_resource = cors.add(self.app.router.add_resource("/wfu/{user}"))
cors.add(wfu_resource.add_route("GET", self.wfu))
users_resource = cors.add(self.app.router.add_resource("/users"))
cors.add(users_resource.add_route("GET", self.users))
self.logger = logging.getLogger()
self.host = host
self.port = port
self.min_amount = 10 if Env.banano() else 0.1
示例14: make_app
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def make_app() -> web.Application:
app = web.Application()
executor = ProcessPoolExecutor()
cors = aiohttp_cors.setup(app)
resource = cors.add(app.router.add_resource("/"))
cors.add(
resource.add_route("POST", partial(handle, executor=executor)),
{
"*": aiohttp_cors.ResourceOptions(
allow_headers=(*BLACK_HEADERS, "Content-Type"), expose_headers="*"
)
},
)
return app
示例15: run_server
# 需要導入模塊: import aiohttp_cors [as 別名]
# 或者: from aiohttp_cors import ResourceOptions [as 別名]
def run_server(port, host, database, static_folder,
wrapmap, wkspace, template_path):
global db, workspace, wrapper_map
db = database
workspace = wkspace
wrapper_map = wrapmap
app = web.Application()
app.router.add_post('/login', login)
app.router.add_post('/logout', logout)
app.router.add_post('/signup', signup)
app.router.add_post('/attempt', new_attempt)
app.router.add_post('/question', question)
app.router.add_get('/languages', languages)
app.router.add_post('/score', score_calc)
app.router.add_post('/wait/for/verdict', wait_for_verdict)
app.router.add_get('/leader', leaderboard)
app.router.add_get('/', home)
# -----------cors
corsconfig = {"*": aiohttp_cors.ResourceOptions(allow_credentials=True,
expose_headers="*",
allow_headers="*")}
cors = aiohttp_cors.setup(app, defaults=corsconfig)
for route in list(app.router.routes()):
try:
cors.add(route)
except Exception as e:
print(e) # /register will be added twice and will raise error
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(template_path))
x = app.router.add_static('/static', static_folder)
x = cors.add(x)
web.run_app(app, host=host, port=port)