python-aiohttpSetting CORS with Python Aiohttp?
Python Aiohttp supports setting CORS (Cross-Origin Resource Sharing) headers. This allows web applications to access resources from other domains.
Example code
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)
Output example
======== Running on http://0.0.0.0:8080 ========
(Press CTRL+C to quit)
Code explanation
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
Helpful links
Related
- How to handle x-www-form-urlencoded with Python Aiohttp?
- How to create a websocket server using Python Aiohttp?
- How to create a JSON response using Python Aiohttp?
- How to create a connection pool with Python Aiohttp?
- How to check if a session is closed with Python Aiohttp?
- How to create a server with Python Aiohttp?
- How to reuse a session with Python Aiohttp?
- How to download large files with Python Aiohttp?
- How to redirect with Python Aiohttp?
- How to use Gzip with Python Aiohttp?
More of Python Aiohttp
- How to handle x-www-form-urlencoded with Python Aiohttp?
- How to create a websocket server using Python Aiohttp?
- How to redirect with Python Aiohttp?
- How to create a JSON response using Python Aiohttp?
- How to reuse a session with Python Aiohttp?
- How to make parallel requests with Python Aiohttp?
- How to set headers in Python Aiohttp?
- How to get response text with Python Aiohttp?
- How to set query parameters with Python Aiohttp?
- How to use keepalive with Python Aiohttp?
See more codes...