forked from unikraft/catalog
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples): Introduce Python 3 HTTP Server
Introduce Python3 as binary compatibility runstarting an HTTP server. Extract / Install Python using `Dockerfile`. Then run it with the `base` kernel images from `../../kernels/`. Add typical files for a bincompat app: * `Kraftfile`: build / run rules, including pulling the `base` image * `Dockerfile`: filesystem, including binary and libraries * `Makefile`: used to generate the root filesystem from the `Dockerfile` rules * `README.md`: instructions to set up, build and run the application * `config.yaml`: configuration file to generate scripts to the application * `server.py`: Python script to start an HTTP server `config.yaml` is used to generate run scripts using the `../../utils/bincompat/generate.py` script. The kernels in `../../kernels` are generated by running the `../../utils/bincompat/base-build-all.sh` script while inside the `../../library/base/` directory. Signed-off-by: Razvan Deaconescu <[email protected]>
- Loading branch information
Showing
7 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/rootfs/ | ||
/rootfs.cpio | ||
/run-qemu* | ||
/run-fc* | ||
/kraft-run-* | ||
/fc*.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM docker.io/library/python:3.12 as base | ||
|
||
FROM scratch | ||
|
||
COPY --from=base /usr/lib/python3 /usr/lib/python3 | ||
COPY --from=base /usr/bin/python3 /usr/local/bin/python3 | ||
COPY --from=base /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/libc.so.6 | ||
COPY --from=base /lib/x86_64-linux-gnu/libm.so.6 /lib/x86_64-linux-gnu/libm.so.6 | ||
COPY --from=base /lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2 | ||
|
||
COPY ./server.py /server.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
spec: v0.6 | ||
|
||
name: http-python3 | ||
|
||
rootfs: ./Dockerfile | ||
|
||
cmd: ["/usr/local/bin/python3", "/server.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
IMAGE_NAME = unikraft-python3 | ||
|
||
include ../../utils/bincompat/docker.Makefile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# HTTP Server on Python3 | ||
|
||
This directory contains the definition to run an HTTP server with [Python3](https://www.python.org/) on Unikraft in binary compatibility mode. | ||
|
||
Follow the instructions in the common `../README.md` file to set up and configure the application. | ||
|
||
## Quick Run | ||
|
||
Use `kraft` to run the image: | ||
|
||
```console | ||
kraft run -M 512M -p 8080:8080 | ||
``` | ||
|
||
Once executed, it will run the `server.py` script and wait for connections on port `8080`. | ||
|
||
Query the server using: | ||
|
||
```console | ||
curl localhost:8080 | ||
``` | ||
|
||
It will print "Hello, World!". | ||
|
||
## Scripted Run | ||
|
||
Use the scripted runs, detailed in the common `../README.md`. | ||
Once executed, scripts will run the `server.py` script and wait for connections on port `8080`. | ||
|
||
Query the server using: | ||
|
||
```console | ||
curl 172.44.0.2:8080 | ||
``` | ||
|
||
It will print "Hello, World!". | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
networking: True | ||
accel: True | ||
memory: 512 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import argparse | ||
from http.server import HTTPServer, BaseHTTPRequestHandler | ||
|
||
class MyServer(BaseHTTPRequestHandler): | ||
def do_GET(self): | ||
self.send_response(200) | ||
self.send_header("Content-type", "text/html") | ||
self.end_headers() | ||
self.wfile.write(bytes("Hello, world!", "utf-8")) | ||
|
||
def main(args): | ||
server = HTTPServer((args.host, args.port), MyServer) | ||
|
||
print("starting server at %s:%s" % (args.host, args.port)) | ||
|
||
try: | ||
server.serve_forever() | ||
|
||
except KeyboardInterrupt: | ||
pass | ||
|
||
print("server stopped") | ||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--host", type=str, default="0.0.0.0") | ||
parser.add_argument("--port", type=int, default=8080) | ||
return parser.parse_args() | ||
|
||
if __name__ == "__main__": | ||
main(parse_args()) |