{"library":"aiohttp-cors","code":"import aiohttp_cors\nfrom aiohttp import web\n\nasync def handler(request):\n    return web.Response(text=\"Hello from CORS-enabled endpoint!\")\n\nasync def main():\n    app = web.Application()\n\n    # Setup CORS for all origins, allowing all headers and methods\n    cors = aiohttp_cors.setup(app, defaults={\n        \"*\": aiohttp_cors.ResourceOptions(\n            allow_credentials=True,\n            expose_headers=\"*\",\n            allow_headers=\"*\",\n            allow_methods=\"*\"\n        )\n    })\n\n    # Add a route and enable CORS for it\n    resource = cors.add(app.router.add_resource(\"/hello\"))\n    resource.add_route(\"GET\", handler)\n\n    # Alternatively, you can add CORS to existing routes directly\n    # For example, if you had:\n    # app.router.add_post(\"/data\", data_handler)\n    # cors.add(app.router.get(\"/data\"))\n\n    # To apply CORS to ALL existing routes:\n    # for route in list(app.router.routes()):\n    #     if not route.is_cors_enabled():\n    #         cors.add(route)\n\n    print(\"Server starting on http://localhost:8080\")\n    runner = web.AppRunner(app)\n    await runner.setup()\n    site = web.TCPSite(runner, 'localhost', 8080)\n    await site.start()\n    await asyncio.Event().wait() # Keep server running\n\nif __name__ == '__main__':\n    import asyncio\n    try:\n        asyncio.run(main())\n    except KeyboardInterrupt:\n        print(\"Server stopped.\")\n","lang":"python","description":"This quickstart sets up a basic aiohttp web application and applies a permissive CORS policy to a '/hello' endpoint, allowing requests from any origin. It demonstrates how to initialize CORS for the application and then apply it to specific routes or resources. It also shows a commented-out section for applying CORS to all routes programmatically.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":-1},{"runtime":"python:3.10-slim","exit_code":-1},{"runtime":"python:3.11-alpine","exit_code":-1},{"runtime":"python:3.11-slim","exit_code":-1},{"runtime":"python:3.12-alpine","exit_code":-1},{"runtime":"python:3.12-slim","exit_code":-1},{"runtime":"python:3.13-alpine","exit_code":-1},{"runtime":"python:3.13-slim","exit_code":-1},{"runtime":"python:3.9-alpine","exit_code":-1},{"runtime":"python:3.9-slim","exit_code":-1}]}