Guides
Python HTTP server

Quickstart guide for a Python application

In this guide, you'll learn the process of deploying a Python application on Wasmer Edge, in particular a Python http server. We will cover installation of the CLI, setting up a new Python HTTP server, and deploying it.

Deploying a Python application

Install Wasmer

Click here for instructions on how to install Wasmer if you haven't done it already!

Initialize the local directory

With the CLI installed, let's create a new Python application! We begin creating a new empty directory - after that, we will need to run a single command.

$ mkdir my-python-http-server 
$ cd my-python-http-server
$ wasmer deploy --template=python-http-server             
It seems you are trying to create a new app!
 Who should own this app? · <you> 
 What should be the name of the app? · <your-app-name> 
 Unpacked template
 Do you want to deploy the app now? · yes
Loading local package (manifest path: <current-working-directory>)
 Correctly built package locally
 Package correctly uploaded
 Succesfully pushed release to namespace <you> on the registry 
Deploying app <your-app-name> (<you>) to Wasmer Edge...
 
App <your-app-name> (<you>) was successfully deployed 🚀
https://<your-app-name>-<you>.wasmer.app
 
 Unique URL: https://<app_id>.id.wasmer.app
 Dashboard:  https://wasmer.io/apps/<you>/<your-app-name>
 
Waiting for new deployment to become available...
(You can safely stop waiting now with CTRL-C)
.
𖥔 Deployment complete

The wasmer deploy --template=python-http-server command will prompt you for the following:

  • App owner: This is the owner of the app. It can be your username or an organization; if you're logged in, the command will prompt you to choose from your namespaces: by default, it will be your username.
  • App name: This is the name of your app. By default, it will be the name of the current directory.

The above command will do the following:

  • Download the template from the registry
  • Deploy it to Wasmer Edge with the user-provided informations

Let's check it:

$ curl https://<your-app-name>-<you>.wasmer.app
{"message": "Python app is running with Wasmer!"}% 

Update the app

Your directory should now look like this:

    • main.py
  • wasmer.toml
  • README.md
  • LICENSE
  • app.yaml
  • To illustrate the lifecycle of an app, let's edit the main.py file in the src folder:

    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!", # <-- Notice the comma!
                "hello": "world" # <- New line
            }
            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()

    Let's re-deploy our Python HTTP server:

    $ wasmer deploy
    Loading local package (manifest path: <current-working-directory>)
     Correctly built package locally
     Package correctly uploaded
     Succesfully pushed release to namespace <you> on the registry 
    Deploying app <your-app-name> (<you>) to Wasmer Edge...
     
    App <your-app-name> (<you>) was successfully deployed 🚀
    https://<your-app-name>-<you>.wasmer.app
     
     Unique URL: https://<app_id>.id.wasmer.app
     Dashboard:  https://wasmer.io/apps/<you>/<your-app-name>
     
    Waiting for new deployment to become available...
    (You can safely stop waiting now with CTRL-C)
    .
    𖥔 Deployment complete

    Let's try to cURL the server:

    $ curl https://<your-app-name>-<you>.wasmer.app
    {"message": "Python app is running with Wasmer!", "hello": "world"}% 

    Testing your Python application locally

    To run your Python application locally simply run

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

    This will start a local server on http://localhost:9090.

    ℹ️

    You can see all the available options with wasmer run --help or click here to see the full documentation.

    Let's try to cURL the server:

    $ curl http://127.0.0.1:9090
    {"message": "Python app is running with Wasmer!"}% 

    Conclusion

    Congratulations! You have successfully deployed a Python HTTP server on Wasmer Edge 🚀.

    Tip: To make changes to your Python HTTP server, simply modify the main.py file in the src directory and run wasmer deploy again to deploy the changes.

    Resources

    wasmer-examples/python-wasmer-starter