// plain
Python Aiohttp supports setting CORS (Cross-Origin Resource Sharing) headers. This allows web applications to access resources from other domains.
from aiohttp import web
async def handle(request):
response = web.Response()
response.headers['Access-Control-Allow-Origin'] = '*'
return response
app = web.Application()
app.router.add_get('/', handle)
web.run_app(app)
======== Running on http://0.0.0.0:8080 ========
(Press CTRL+C to quit)
from aiohttp import web
: imports the web module from the aiohttp libraryresponse.headers['Access-Control-Allow-Origin'] = '*'
: sets the Access-Control-Allow-Origin header to allow requests from any domainapp.router.add_get('/', handle)
: adds a route to the application that will handle GET requestsweb.run_app(app)
: starts the web application
group: aiohttp
onelinerhub: Setting CORS with Python Aiohttp?