Version 0.3.1

Actually working properly!
This commit is contained in:
Nova
2026-08-10 15:56:44 +02:00
parent 4a93ffa20c
commit f3034575ee
2 changed files with 73 additions and 38 deletions
+51 -22
View File
@@ -38,7 +38,7 @@ TODO: actually put normal comments in
TODO: INPROG: add typing to all code, new code will feature it by default.
"""
# Stable imports go here
import sys
import threading
import os
@@ -47,6 +47,9 @@ import threading
import ssl
import socket
import signal
# Experimental imports go here
import select
import subprocess
try:
if not os.getcwd() in sys.path:
@@ -60,7 +63,7 @@ except ImportError:
)
# pass
AMETHYST_BUILD_NUMBER = "0.3.0-0114-mt-tryout1"
AMETHYST_BUILD_NUMBER = "0.3.1-0130-mt-tryout2"
AMETHYST_REPO = "https://git.novacow.ch/Nova/PyWebServer/"
@@ -422,6 +425,8 @@ class WebServer:
self.key_file = self.file_handler.read_config("key") or key_file
self.max_length = int(self.file_handler.read_config("max-length")) or 8192
self.skip_ssl = False
self.threading = bool(self.file_handler.read_config("threading"))
self.tlock = threading.Lock()
# me when no certificate and key file
if not os.path.exists(self.cert_file) or not os.path.exists(self.key_file):
@@ -449,11 +454,11 @@ class WebServer:
self.http_socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
self.http_socket.bind(("::", self.http_port))
self.http_socket.settimeout(25)
# self.http_socket.settimeout(1)
self.https_socket_raw = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
self.https_socket_raw.bind(("::", self.https_port))
self.https_socket_raw.settimeout(25)
# self.https_socket_raw.settimeout(1)
self.proxy_handler = ProxyServer(self.file_handler)
@@ -486,6 +491,8 @@ class WebServer:
self.running = True
def start(self, http, https):
signal.signal(signal.SIGINT, self.shutdown)
signal.signal(signal.SIGTERM, self.shutdown)
if https is True:
if self.skip_ssl is True:
print("WARN: You have enabled HTTPS without SSL!!")
@@ -504,10 +511,18 @@ class WebServer:
self.http_socket.listen(5)
print(f"HTTP server listening on port {self.http_port}...")
while self.running:
print(f"State of self.running: {self.running}")
try:
ready, _, _ = select.select(
[self.http_socket],
[],
[],
1.0
)
if not ready:
continue
conn, addr = self.http_socket.accept()
if self.file_handler.read_config("threading") is True:
conn.settimeout(2)
if self.threading:
threading.Thread(
target=self.handle_connection,
args=(conn, addr),
@@ -515,24 +530,32 @@ class WebServer:
).start()
else:
self.handle_connection(conn, addr)
except socket.timeout:
continue
except OSError as e:
if f"{e}" == "timed out":
continue
else:
print(f"OSError! {e}")
if not self.running:
break
print(f"OSError! {e}")
continue
except Exception as e:
if not "timeout" in f"{e}":
print(f"HTTP error: {e}")
print(f"HTTP error: {e}")
def start_https(self):
self.https_socket.listen(5)
print(f"HTTPS server listening on port {self.https_port}...")
while self.running:
print(f"State of self.running: {self.running}")
try:
ready, _, _ = select.select(
[self.https_socket],
[],
[],
1.0
)
if not ready:
continue
conn, addr = self.https_socket.accept()
if self.file_handler.read_config("threading") is True:
conn.settimeout(2)
if self.threading:
threading.Thread(
target=self.handle_connection,
args=(conn, addr),
@@ -540,15 +563,15 @@ class WebServer:
).start()
else:
self.handle_connection(conn, addr)
except socket.timeout:
continue
except OSError as e:
if f"{e}" == "timed out":
continue
else:
print(f"OSError! {e}")
if not self.running:
break
print(f"OSError! {e}")
continue
except Exception as e:
if not "timeout" in f"{e}":
print(f"HTTPS error: {e}")
print(f"HTTPS error: {e}")
def handle_connection(self, conn, addr):
try:
@@ -689,7 +712,14 @@ class WebServer:
try:
pesclass = pes.PES()
threadcompat = pesclass.THREAD_SAFETY
# if not threadcompat:
if not threadcompat and self.threading is True:
print(
"PES is not thread-safe yet threading is enabled!\n"
"Amethyst CANNOT guarantee data intergity!\n"
"It is HIGHLY recommended you make your script thread-safe!\n"
)
with self.tlock:
return pesclass.on_request(data)
return pesclass.on_request(data)
except Exception as e:
return self.build_response(
@@ -815,7 +845,6 @@ class WebServer:
self.running = False
self.http_socket.close()
self.https_socket.close()
sys.exit(0)
def main():