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!
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
Deploy!
We begin creating a new empty directory.
mkdir my-new-python-app
cd my-new-python-app
Then, running a single command, we can setup a python http server.
wasmer deploy --template=python-http-server
This 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 tochoose 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.
Expect to see output similar to this:
It seems you are trying to create a new app!
✔ Who should own this app? · wasmer-user
✔ What should be the name of the app? · python-http-server
✔ Unpacked template
✔ Do you want to deploy the app now? · yes
Loading local package (manifest path: /tmp/tmp.hHSLc0Lr49/.)
✔ Correctly built package locally
✔ Package correctly uploaded
✔ Succesfully pushed release to namespace wasmer-user on the registry
Deploying app python-http-server (wasmer-user) to Wasmer Edge...
App python-http-server (wasmer-user) was successfully deployed 🚀
https://python-http-server-wasmer-user.wasmer.app
→ Unique URL: https://r3dhkilce6k0.id.wasmer.app
→ Dashboard: https://wasmer.io/apps/wasmer-user/python-http-server
Waiting for new deployment to become available...
(You can safely stop waiting now with CTRL-C)
.
𖥔 Deployment complete
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://python-http-server-wasmer.user.app
The deployment URL follows the format https://<app-name>-<app-owner>.wasmer.app
Expect output similar to this:
{"message": "Python app is running with Wasmer!"}
Update the app
Your directory should now look like this:
- main.py
To illustrate the lifecycle of an app, let's edit the main.py
file
in the src
folder:
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
Once it's successfully redeployed, let's try to cURL the server:
curl https://python-http-server-wasmer.user.app
Expect it to contain the additional data field:
{"message": "wasmer/python-http-server is running on Python!", "hello": "world"}
Testing your Python application locally
To run your Python application locally simply run
wasmer run . --env PORT=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
Expect:
{"message": "wasmer/python-http-server is running on Python!", "hello": "world"}
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 thesrc
directory and runwasmer deploy
again to deploy the changes.