Edge
Quick Start
Python

Quickstart guide for deploying a Python Application

In this guide, you'll learn the process of deploying a python application on Wasmer Edge. We'll be deploying a basic http server written in python.

Deploying a Python Application

Install Wasmer CLI

Install the latest version of Wasmer CLI following the instructions here.

ℹ️

You can see all commands available with wasmer --help. You can also read the CLI documentation online.

⚠️

Please check that you have the latest version of wasmer runtime as this tutorial depends on version 4.2.3 or higher.

Log in into Wasmer

Create a new account in Wasmer (opens in a new tab). Then, log in into the Wasmer CLI and follow the provided steps to provide the CLI access to your Wasmer account.

wasmer login

Step 3.1: Initialize the Python Application Starter template

$ wasmer app create
App type:
  Static website
  HTTP server
  Browser shell
  JS Worker (experimental)
> Python Application
ℹ️

This is an interactive command. You can also use the --type flag to specify the app type.

Further you will be prompted to enter your package name and app name. You can choose any name you like.

🚧

The app names should be globally unique across all apps on the registry.

You directory composition should look like this:

Your main.py file should look like this:

src/main.py
import os
import json
from http.server import SimpleHTTPRequestHandler, HTTPServer
 
 
class CustomHandler(SimpleHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()
 
        data = {
            "message": "wasmer/python-http-server is running on Python!"
        }
        self.wfile.write(json.dumps(data).encode('utf-8'))
 
 
if __name__ == "__main__":
    host = os.environ.get('HOST', '127.0.0.1')
 
    # Get the PORT environment variable if it's present; otherwise, default to 8000
    port = int(os.environ.get('PORT', 80))
 
    server = HTTPServer((host, port), CustomHandler)
    print(f"Starting server on http://{host}:{port}")
    server.serve_forever()

You can change the content of the main.py file to your liking.

Step 3.2: Testing your Python Application locally

Run the commands below to initialize files for Wasmer Edge:

$ wasmer run . --net --env PORT=4333
Starting server on http://127.0.0.1:4333

The above command will start a web server on http://127.0.0.1:4333.

Let's try to cURL the server:

$ curl http://127.0.0.1:4333
"message": "wasmer/python-http-server is running on Python!"

Step 3.3: Deploying your Python Application

Deploying is the easiest part. Just run the following command:

$ wasmer deploy
Deploying app wasmer/python-http-server...
 
  App wasmer/python-http-server was successfully deployed!
 
> App URL: https://wasmer-python-http-server.wasmer.app
> Versioned URL: https://rkkh7ikcgv1r.id.wasmer.app
> Admin dashboard: https://wasmer.io/apps/wasmer/wasmer-python-http-server
ℹ️

You must be in the directory holding the wasmer.toml and app.yaml config files.

💁

You can view the above info again using wasmer app info.

Conclusion

Congratulations! You have successfully deployed your python application on Wasmer Edge.

Resources

wasmerio/edge-py-http-server