Examples
Cloudflare has a wide range of Python examples in the Workers Example gallery.
In addition to those examples, consider the following ones that illustrate Python-specific behavior.
from workers import WorkerEntrypoint, Responsefrom urllib.parse import urlparse, parse_qs
class Default(WorkerEntrypoint):    async def fetch(self, request):        # Parse the incoming request URL        url = urlparse(request.url)        # Parse the query parameters into a Python dictionary        params = parse_qs(url.query)
        if "name" in params:            greeting = "Hello there, {name}".format(name=params["name"][0])            return Response(greeting)
        if url.path == "/favicon.ico":          return Response("")
        return Response("Hello world!")from workers import WorkerEntrypoint, Response
class Default(WorkerEntrypoint):    async def fetch(self, request):        name = (await request.json()).name        return Response("Hello, {name}".format(name=name))# To use the JavaScript console APIsfrom js import consolefrom workers import WorkerEntrypoint, Response# To use the native Python loggingimport logging
class Default(WorkerEntrypoint):    async def fetch(self, request):        # Use the console APIs from JavaScript        # https://developer.mozilla.org/en-US/docs/Web/API/console        console.log("console.log from Python!")
        # Alternatively, use the native Python logger        logger = logging.getLogger(__name__)
        # The default level is warning. We can change that to info.        logging.basicConfig(level=logging.INFO)
        logger.error("error from Python!")        logger.info("info log from Python!")
        # Or just use print()        print("print() from Python!")
        return Response("We're testing logging!")from js import Objectfrom pyodide.ffi import to_js as _to_js
from workers import WorkerEntrypoint, Response
# to_js converts between Python dictionaries and JavaScript Objectsdef to_js(obj):   return _to_js(obj, dict_converter=Object.fromEntries)
class Default(WorkerEntrypoint):    async def fetch(self, request):        # Bindings are available on the 'env' attribute        # https://developers.cloudflare.com/queues/
        # The default contentType is "json"        # We can also pass plain text strings        await self.env.QUEUE.send("hello", contentType="text")        # Send a JSON payload        await self.env.QUEUE.send(to_js({"hello": "world"}))
        # Return a response        return Response.json({"write": "success"})from workers import WorkerEntrypoint, Response
class Default(WorkerEntrypoint):    async def fetch(self, request):        results = await self.env.DB.prepare("PRAGMA table_list").run()        # Return a JSON response        return Response.json(results)Refer to Query D1 from Python Workers for a more in-depth tutorial that covers how to create a new D1 database and configure bindings to D1.
- If you're new to Workers and Python, refer to the get started guide
 - Learn more about calling JavaScript methods and accessing JavaScript objects from Python
 - Understand the supported packages and versions currently available to Python Workers.
 
Was this helpful?
- Resources
 - API
 - New to Cloudflare?
 - Directory
 - Sponsorships
 - Open Source
 
- Support
 - Help Center
 - System Status
 - Compliance
 - GDPR
 
- Company
 - cloudflare.com
 - Our team
 - Careers
 
- © 2025 Cloudflare, Inc.
 - Privacy Policy
 - Terms of Use
 - Report Security Issues
 - Trademark