Skip to content

Commit 667ac92

Browse files
authored
feat(examples): Add python 3.14 example (#250)
Reviewed-by: Razvan Deaconescu <razvand@unikraft.io> Approved-by: Razvan Deaconescu <razvand@unikraft.io>
2 parents f4b3bc2 + 0bbdb2d commit 667ac92

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM python:3.14 AS base
2+
3+
FROM scratch
4+
5+
COPY --from=base /usr/local/lib/python3.14 /usr/local/lib/python3.14
6+
7+
COPY ./server.py /server.py
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spec: v0.6
2+
3+
name: httpserver-python3.14
4+
5+
runtime: python:3.14
6+
7+
rootfs: ./Dockerfile
8+
9+
cmd: ["/usr/bin/python", "/server.py"]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Python HTTP Server
2+
3+
This directory contains a [Python](https://www.python.org/) HTTP server running on Unikraft that provides a simple response to each request.
4+
5+
## Set Up
6+
7+
To run this example, [install Unikraft's companion command-line toolchain `kraft`](https://unikraft.org/docs/cli), clone this repository and `cd` into this directory.
8+
9+
## Run and Use
10+
11+
Use `kraft` to run the image and start a Unikraft instance:
12+
13+
```bash
14+
kraft run --rm -p 8080:8080 --plat qemu --arch x86_64 -M 512M .
15+
```
16+
17+
If the `--plat` argument is left out, it defaults to `qemu`.
18+
If the `--arch` argument is left out, it defaults to your system's CPU architecture.
19+
20+
Once executed, it will open port `8080` and wait for connections.
21+
To test it, you can use `curl`:
22+
23+
```bash
24+
curl localhost:8080
25+
```
26+
27+
You should see a "Bye, World!" message.
28+
29+
## Inspect and Close
30+
31+
To list information about the Unikraft instance, use:
32+
33+
```bash
34+
kraft ps
35+
```
36+
37+
```text
38+
NAME KERNEL ARGS CREATED STATUS MEM PORTS PLAT
39+
inspiring_davidgreybeard oci://unikraft.org/python:3.14 /server.py 16 seconds ago running 488M 0.0.0.0:8080->8080/tcp qemu/x86_64
40+
```
41+
42+
The instance name is `inspiring_dadidgreybeard`.
43+
To close the Unikraft instance, close the `kraft` process (e.g., via `Ctrl+c`) or run:
44+
45+
```bash
46+
kraft rm inspiring_dadidgreybeard
47+
```
48+
49+
Note that depending on how you modify this example your instance **may** need more memory to run.
50+
To do so, use the `kraft run`'s `-M` flag, for example:
51+
52+
```bash
53+
kraft run --rm -p 8080:8080 --plat qemu --arch x86_64 -M 1024M .
54+
```
55+
56+
## `kraft` and `sudo`
57+
58+
Mixing invocations of `kraft` and `sudo` can lead to unexpected behavior.
59+
Read more about how to start `kraft` without `sudo` at [https://unikraft.org/sudoless](https://unikraft.org/sudoless).
60+
61+
## Learn More
62+
63+
- [How to run unikernels locally](https://unikraft.org/docs/cli/running)
64+
- [Building `Dockerfile` Images with `BuildKit`](https://unikraft.org/docs/getting-started/integrations/buildkit)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import argparse
2+
from http.server import HTTPServer, BaseHTTPRequestHandler
3+
4+
class MyServer(BaseHTTPRequestHandler):
5+
def do_GET(self):
6+
self.send_response(200)
7+
self.send_header("Content-type", "text/html")
8+
self.end_headers()
9+
self.wfile.write(bytes("Bye, World!", "utf-8"))
10+
11+
def main(args):
12+
server = HTTPServer((args.host, args.port), MyServer)
13+
14+
print("starting server at %s:%s" % (args.host, args.port))
15+
16+
try:
17+
server.serve_forever()
18+
19+
except KeyboardInterrupt:
20+
pass
21+
22+
print("server stopped")
23+
24+
def parse_args():
25+
parser = argparse.ArgumentParser()
26+
parser.add_argument("--host", type=str, default="0.0.0.0")
27+
parser.add_argument("--port", type=int, default=8080)
28+
return parser.parse_args()
29+
30+
if __name__ == "__main__":
31+
main(parse_args())

0 commit comments

Comments
 (0)