CF-Changes Workers - Python Workers now support WSGI web frameworks like Django and Flask

Cloudflare Reporting - Product Changelog

ENF News Bot

Automated News Bot
Joined
Apr 15, 2022
Messages
4,195
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​

https://developers.cloudflare.com/changelog/rss/#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​

https://developers.cloudflare.com/changelog/rss/#wsgi-frameworks

For WSGI frameworks like Django or Flask:


Code:
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:


Code:
from workers import wsgi, WorkerEntrypoint

class Default(WorkerEntrypoint):
    async def fetch(self, request):
        return await wsgi.fetch(app, request, self.env)

ASGI frameworks​

https://developers.cloudflare.com/changelog/rss/#asgi-frameworks

For ASGI frameworks like FastAPI or Starlette:


Code:
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.

Continue reading...
 
Back
Top