Package: asgi-cors
asgi-corsASGI middleware for applying CORS headers to an ASGI application.Installationpip install asgi-corsSome background on CORSCORS stands for Cross-Origin Resource Sharing. It is a web standard that allows applications to opt-in to allowing JavaScript running on other domains to makefetch()calls that can retrieve data from the application.SeeMDN's CORS articlefor more background.The easiest way to allow scripts running on other domains to access data from an application is to add the following HTTP header:Access-Control-Allow-Origin: *This will allow scripts running on ANY domain to makefetch()calls against the application. For public data this is often fine, but there are situations where this may not be what you want to do: one example might be code that runs behind a VPN and needs to allow specific, trusted hosts to load data without opening itself up to every site on the internet.For these cases, the server needs to inspect the Origin header from the client and return that Origin in the above header. For example, an incoming request fromhttp://localhost:8000might be judged as trusted - in which case the application server needs to reply like so:Access-Control-Allow-Origin: http://localhost:8000Note that theAccess-Control-Allow-Originheader can only return a single value. This means that if you want to allow requests from multiple origins you need to dynamically whitelist those origins and return a different header value depending on the incoming request.How to use this middlewareWe will assume you have an existing ASGI app, in a variable calledapp.First, import theasgi_corsfunction:from asgi_cors import asgi_corsTo enable CORS headers for everywhere (by adding theAccess-Control-Allow-Origin: *header to every request), do this:app = asgi_cors(app, allow_all=True)If you wish to only allow it from a specific host, use the following:app = asgi_cors(app, hosts=[
    "https://www.example.com"
])Now JavaScript executing onhttps://www.example.comwill be able to call your API. You can test this out by opening up example.com in your browser, opening your browser's devtools console and pasting in the following JavaScript:fetch("https://your-api.com/").then(r => r.json()).then(d => console.log(d))You can include multiple hosts in the list.Finally, if you want to open your application up to requests from a wildcard-defined selection of hosts, use the following:app = asgi_cors(app, host_wildcards=[
    "http://localhost:800*",
    "http://*.example.com"
])This will enable access from any JavaScript running on a local host server on ports 8000 through 8009 - or from any subdomain of example.com.If you need to do something more complicated that cannot be expressed using thehosts=orhost_wildcards=parameters, you can usecallback=to specify a custom function. For example:def validate_origin(origin):
    return origin.startswith("https://")

app = asgi_cors(app, callback=validate_origin)Your callback function will be passed theOriginheader that was passed in by the browser.Using the middleware as a decoratorIf you are defining your ASGI application directly as a function, you can use theasgi_cors_decoratorfunction decorator like so:from asgi_cors import asgi_cors_decorator


@asgi_cors_decorator(allow_all=True)
async def my_asgi_app(scope, recieve, send):
    # Your app goes here