Coverage for lst_auto_rta/Server_RTA_Multiple_Images.py: 0%
44 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-03 14:47 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-03 14:47 +0000
1#!/usr/bin/env python
3import argparse
4import os
5from http.server import BaseHTTPRequestHandler, HTTPServer
6from io import BytesIO
8import numpy as np
10parser = argparse.ArgumentParser()
11parser.add_argument("directory", type=str, help="directory path containing the images")
12args = parser.parse_args()
15class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
16 def do_GET(self):
17 try:
18 if self.path == "/":
19 file_paths = self.get_image_paths()
20 page_content = self.generate_html_page(file_paths)
21 self.send_response(200)
22 self.send_header("Content-type", "text/html")
23 self.send_header("Content-length", len(page_content))
24 self.end_headers()
25 self.wfile.write(page_content.encode())
26 else:
27 file_path = os.path.join(args.directory, os.path.basename(self.path))
28 if os.path.isfile(file_path):
29 image = open(file_path, "rb").read()
30 self.send_response(200)
31 self.send_header("Content-type", "image/jpeg")
32 self.send_header("Content-length", len(image))
33 self.end_headers()
34 self.wfile.write(BytesIO(image).getvalue())
35 else:
36 self.send_error(404, "File Not Found: %s" % self.path)
37 except Exception as e:
38 self.send_error(500, "Internal Server Error: %s" % str(e))
40 def get_image_paths(self):
41 image_paths = []
42 for filename in sorted(os.listdir(args.directory)):
43 if filename.lower().endswith((".jpg", ".jpeg", ".png", ".gif")):
44 image_paths.append(filename)
45 return image_paths
47 def generate_html_page(self, image_paths):
48 html = """
49 <!DOCTYPE html>
50 <html>
51 <head>
52 <title>RTA engineering GUI</title>
53 <style>
54 .image {
55 width: 1200px; /* Adjust the width as desired */
56 height: auto; /* Keeps the aspect ratio */
57 }
58 </style>
59 </head>
60 <body>
61 <h1>RTA engineering GUI</h1>
62 """
64 for image_path in image_paths:
65 html += f'<img src="/{image_path}" alt="{image_path}" class="image"><br>'
67 html += """
68 </body>
69 </html>
70 """
72 return html
75httpd = HTTPServer(("0.0.0.0", 38100), SimpleHTTPRequestHandler)
76httpd.serve_forever()