Python Workers now support WSGI and ASGI frameworks
Python Workers now support WSGI and ASGI web frameworks, enabling you to use popular frameworks like Django, Flask, FastAPI, and Starlette. You can use the wsgi.entrypoint or asgi.entrypoint utilities to integrate these frameworks with your Workers applications.
Python web frameworks following the Web Server Gateway Interface (WSGI) ↗ or Asynchronous Server Gateway Interface (ASGI) ↗ specification can now be used in Python Workers.
Using web frameworks with Python Workers
Based on the web framework you are using, you can use either wsgi or asgi from the workers module.
WSGI frameworks
For WSGI frameworks like Django or Flask:
from workers import wsgi
from django.core.wsgi import get_wsgi_application
app = get_wsgi_application()
Default = wsgi.entrypoint(app)
The wsgi.entrypoint is equivalent to creating a WorkerEntrypoint class and using the wsgi.fetch method. If you want more control over the WorkerEntrypoint class, you can do so:
from workers import wsgi, WorkerEntrypoint
class Default(WorkerEntrypoint):
async def fetch(self, request):
return await wsgi.fetch(app, request, self.env)
ASGI frameworks
For ASGI frameworks like FastAPI or Starlette:
from workers import asgi
from fastapi import FastAPI
app = FastAPI()
Default = asgi.entrypoint(app)
For more information about using individual web frameworks, refer to the packages documentation in Python Workers.
Source: original entry ↗
More from Cloudflare
Follow Cloudflare to get its new changes in your feed and email digest.
Cloudflare One Client for Windows hotfix - trusted certificate chains
A new GA release for Windows Cloudflare One Client is available, addressing an issue with building trusted certificate chains.
Cloudflare One Client for macOS - Trusted Certificate Chain Fix
A new GA release of the macOS Cloudflare One Client is available. This hotfix addresses an issue with building trusted certificate chains.
Cloudflare One Client for Linux
A new GA release of Cloudflare One Client for Linux is now available. This hotfix addresses an issue with building trusted certificate chains.