Python Cloudflare Workers

Cloudflare Workers

Clouflare Workers, provides a serverless environment without configuring or maintaining infrastructure.

You can create new applications or reuse existing ones.

Initially Cloudflare Workers only allowed you to code in JavaScript and languages that compile to WebAssembly (Rust,C, C++) to develop applications.

However, they now support a broad range of languages.

One of those languages is Python.

In this tutorial I shall demonstrate how to setup a Python orientated Cloudflare Workers development environment.

Cloudflare Workers account

You can sign up for a free Workers account if you don’t have one yet.

Install the Cloudflare Workers CLI

Make sure you have npm installed before running the command below.

npm install -g @cloudflare/wrangler

Generate project

wrangler generate my-python-project https://github.com/cloudflare/python-worker-hello-world

The starter project is hosted inside a GitHub repository.

This is designed to be a starting point for building a new Cloudflare Worker project.

Install Transcript

Cloudflare Workers support for Python requires Transcript.

Transcript converts Python code into JavaScript.

cd my-python-project
virtualenv env
source env/bin/activate
pip install transcrypt

index.py

def handleRequest(request):
    return __new__(Response('Python Worker hello world!', {
        'headers' : { 'content-type' : 'text/plain' }
    }))

addEventListener('fetch', (lambda event: event.respondWith(handleRequest(event.request))))

addEventListener()

A Cloudflare Worker runtime API.

This function defines a trigger for a Cloudflare Worker script to execute.

There are 2 types of event listeners "fetch" and "scheduled".

Preview changes

You can preview any changes to code before deploying to a Worker.

By running wrangler dev a local webserver is served on port 8787.

This is useful for debugging and testing changes locally.

wrangler dev
✨  Built successfully, built project size is 6 KiB.
👂  Listening on http://127.0.0.1:8787
🌀  Detected changes...
💁  Ignoring stale first change

curl the URL, and “Python Worker hello world!” will be returned in plain text.

curl http://localhost:8787
Python Worker hello world!

Publishing changes

In order to deploy your Workers project onto the Cloudflare network, Wrangler needs configuring using details from your Workers account.

2 items are required.

  1. Account ID
  2. API token, or both email and Global API Key
wrangler config

To find your API Token, go to https://dash.cloudflare.com/profile/api-tokens and create it using the "Edit Cloudflare Workers" template.
Consider using `wrangler login` which only requires your Cloudflare username and password.

If you are trying to use your Global API Key instead of an API Token           
(Not Recommended), run `wrangler config --api-key`.                    

Enter API Token: 
...

Update wranger.toml

Further configuration.

cat wrangler.toml

name = "my-python-project"
type = "webpack"
account_id = $yourAccountId
workers_dev = true
route = ""
zone_id = ""
webpack_config = "webpack.config.js"

With the workers_dev key in wrangler.toml set to true, Wrangler will publish your project to your workers.dev subdomain.

With Wrangler configured you can now publish your Worker.

wrangler publish

This will publish to a free Workers dev domain.

e.g. https://$project_name.workers.dev

Or a custom domain.

Further reading

Workers - Getting started

Last updated on 8 Oct 2020
Published on 8 Oct 2020