Add progress & test headers

This commit is contained in:
Gabriel Augendre 2021-05-27 09:44:46 +02:00
parent e0ce5aa66a
commit a6c906e5f5
3 changed files with 125 additions and 0 deletions

62
progress/progress.py Normal file
View file

@ -0,0 +1,62 @@
import datetime
import re
import subprocess
DISK_SIZE_GB = 5.44
def sizeof_fmt(num: int, suffix: str = 'B') -> str:
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
if abs(num) < 1024.0:
return "%3.1f %s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
def time_fmt(_time):
hours = int(_time / 3600)
minutes = int((_time % 3600) / 60)
seconds = _time % 60
fmt = f"{hours}h {minutes}min {seconds}s"
return fmt
def main():
subprocess.run(["sudo", "killall", "-SIGINFO", "dd"])
print("Input last dd progress line")
last_line = input("> ")
regex = re.compile(
r"^(?P<transferred>\d+) \w+ transferred in (?P<elapsed>\d+).\d+ secs \((?P<speed>\d+) \w+/sec\)$")
results = regex.match(last_line).groupdict()
disk_size = DISK_SIZE_GB * 1024 * 1024 * 1024
transferred = int(results["transferred"])
transferred_fmt = sizeof_fmt(transferred)
speed = int(results["speed"])
speed_fmt = sizeof_fmt(speed)
remaining = disk_size - transferred
remaining_fmt = sizeof_fmt(remaining)
eta = int(remaining / speed)
eta_fmt = time_fmt(eta)
elapsed = int(results["elapsed"])
elapsed_fmt = time_fmt(elapsed)
total = eta + elapsed
total_fmt = time_fmt(total)
eta_absolute = datetime.datetime.now() + datetime.timedelta(seconds=eta)
percentage = transferred / disk_size * 100
print()
print(f"{transferred_fmt} transferred over {DISK_SIZE_GB}GiB, or {percentage:.1f}%.")
print(f"Transferring at {speed_fmt} per second.")
print(f"Remaining : {remaining_fmt}")
print(f"ETA : {eta_fmt} ({eta_absolute.ctime()}).")
print(f"Elapsed : {elapsed_fmt}.")
print(f"Estimated total time : {total_fmt}.")
if __name__ == '__main__':
main()

49
test_headers/app.py Normal file
View file

@ -0,0 +1,49 @@
#!/usr/bin/env python3
"""
Very simple HTTP server in python for logging requests
Usage::
./server.py [<port>]
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
class S(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
self._set_response()
self.wfile.write("GET request for {}".format(self.path).encode('utf-8'))
def do_POST(self):
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = self.rfile.read(content_length) # <--- Gets the data itself
logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
str(self.path), str(self.headers), post_data.decode('utf-8'))
self._set_response()
self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))
def run(server_class=HTTPServer, handler_class=S, port=8080):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
logging.info('Starting httpd...\n')
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
logging.info('Stopping httpd...\n')
if __name__ == '__main__':
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()

View file

@ -0,0 +1,14 @@
version: "2.4"
services:
app:
image: python:3.9-slim
command: python /app/app.py
volumes:
- ./:/app
networks:
- nginx-proxy-manager_default
networks:
nginx-proxy-manager_default:
external: true