-
Notifications
You must be signed in to change notification settings - Fork 1
/
servexz.py
executable file
·46 lines (40 loc) · 1.31 KB
/
servexz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
import lzma
import http.server
import os
import socketserver
import shutil
PORT = int(os.environ.get("PORT", 8000))
DIR = os.environ.get("DIR", "/data")
DISK_FILE = os.environ.get("DISK_FILE", "/data/disk.img")
DISK_DEV = os.environ.get("DISK_DEV", "/dev/vmdisk")
COPY_BUFSIZE = 64 * 1024
class XZHandler(http.server.SimpleHTTPRequestHandler):
def copyfile(self, source, outputfile):
length = COPY_BUFSIZE
with lzma.open(outputfile, mode="wb", format=lzma.FORMAT_XZ) as outputxz:
read = source.read
write = outputxz.write
while True:
buf = read(length)
if not buf:
break
write(buf)
def find_vm_disk(self):
if os.path.exists(DISK_FILE):
return open(DISK_FILE, "rb")
elif os.path.exists(DISK_DEV):
return open(DISK_DEV, "rb")
else:
raise Exception("Unable to find VM disk")
def do_GET(self):
f = self.find_vm_disk()
try:
self.send_response(http.server.HTTPStatus.OK)
self.end_headers()
self.copyfile(f, self.wfile)
finally:
f.close()
with socketserver.TCPServer(("", PORT), XZHandler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()