Write Cloudflare Workers in Python · Cloudflare Workers docs | Latest TMZ Celebrity News & Gossip | Watch TMZ Live Skip to content
Cloudflare Docs

Python

Cloudflare Workers provides first-class support for Python, including support for:

Get started

Terminal window
git clone https://github.com/cloudflare/python-workers-examples
cd python-workers-examples/01-hello
npx wrangler@latest dev

A Python Worker can be as simple as three lines of code:

from workers import Response
def on_fetch(request):
return Response("Hello World!")

Similar to Workers written in JavaScript, TypeScript, or Rust, the main entry point for a Python worker is the fetch handler. In a Python Worker, this handler is named on_fetch.

To run a Python Worker locally, you use Wrangler, the CLI for Cloudflare Workers:

Terminal window
npx wrangler@latest dev

To deploy a Python Worker to Cloudflare, run wrangler deploy:

Terminal window
npx wrangler@latest deploy

Modules

Python workers can be split across multiple files. Let's create a new Python file, called src/hello.py:

def hello(name):
return "Hello, " + name + "!"

Now, we can modify src/entry.py to make use of the new module.

from hello import hello
from workers import Response
def on_fetch(request):
return Response(hello("World"))

Once you edit src/entry.py, Wrangler will automatically detect the change and reload your Worker.

The Request Interface

The request parameter passed to your fetch handler is a JavaScript Request object, exposed via the foreign function interface, allowing you to access it directly from your Python code.

Let's try editing the worker to accept a POST request. We know from the documentation for Request that we can call await request.json() within an async function to parse the request body as JSON. In a Python Worker, you would write:

from workers import Response
from hello import hello
async def on_fetch(request):
name = (await request.json()).name
return Response(hello(name))

Once you edit the src/entry.py, Wrangler should automatically restart the local development server. Now, if you send a POST request with the appropriate body, your Worker should respond with a personalized message.

Terminal window
curl --header "Content-Type: application/json" \
--request POST \
--data '{"name": "Python"}' http://localhost:8787
Hello, Python!

The env Parameter

In addition to the request parameter, the env parameter is also passed to the Python fetch handler and can be used to access environment variables, secrets,and bindings.

For example, let us try setting and using an environment variable in a Python Worker. First, add the environment variable to your Worker's Wrangler configuration file:

{
"name": "hello-python-worker",
"main": "src/entry.py",
"compatibility_flags": [
"python_workers"
],
"compatibility_date": "2024-03-20",
"vars": {
"API_HOST": "example.com"
}
}

Then, you can access the API_HOST environment variable via the env parameter:

from workers import Response
async def on_fetch(request, env):
return Response(env.API_HOST)

Further Reading

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.