From 7ac160f6259e637c2337a672e56f105f4cdd2d2a Mon Sep 17 00:00:00 2001 From: Nova Date: Sun, 13 Apr 2025 13:27:58 +0200 Subject: [PATCH] Quick fix that adresses #1 --- README.md | 35 +++++++++++++++++++++++++++++++++++ pywebsrv.py | 15 ++++++++++----- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 66dc559..3f29d22 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,37 @@ # PyWebServer +## Installing +Installing and running PyWebServer is very simple. +Assuming you're running Linux: +```bash +git clone https://git.novacow.ch/Nova/PyWebServer.git +cd ./PyWebServer/ +``` +Windows users, make sure you have installed Git, from there: +```powershell +git clone https://git.novacow.ch/Nova/PyWebServer.git +Set-Location .\PyWebServer\ +``` +From here, you should check from what directory you want to store the content in. +In this example, we'll use `./html/` (or `.\html\` for Windows users) from the perspective of the PyWebServer root dir. +To create this directory, do this: +```bash +mkdir ./html/ +``` +(This applies to both Windows and Linux) +Then, open `pywebsrv.conf` in your favorite text editor and change the `directory` key to the full path to the `./html/` you just created. +After that, put your files in and run this: +Linux: +```bash +python3 /path/to/pywebsrv.py +``` +Windows: +```powershell +# If you have installed Python via the Microsoft Store: +python3 \path\to\pywebsrv.py +# Via the python.org website: +py \path\to\pywebsrv.py +``` + +## SSL Support +Currently PyWebServer warns about AutoCertGen not being installed. AutoCertGen currently is very unstable at the moment, diff --git a/pywebsrv.py b/pywebsrv.py index 122da08..fa40f93 100644 --- a/pywebsrv.py +++ b/pywebsrv.py @@ -45,8 +45,8 @@ class FileHandler: ) def __init__(self, base_dir=None): - self.base_dir = base_dir or os.path.join(os.getcwd(), "html") self.config_path = os.path.join(os.getcwd(), self.CONFIG_FILE) + self.base_dir = self.read_config("directory") def check_first_run(self): if not os.path.isfile(self.config_path): @@ -125,6 +125,11 @@ class FileHandler: or option == "allow-nohost" ): return bool(int(value)) + if option == "directory": + if value == "": + print("FATAL: You haven't set up PyWebServer! Please edit pywebsrv.conf!") + exit(1) + return value return value return None @@ -150,11 +155,11 @@ class RequestParser: """Parses the HTTP request line.""" try: method, path, version = line.split(" ") - if path.endswith("/"): - path += "index.html" - return method, path, version except ValueError: return None, None, None + if path.endswith("/"): + path += "index.html" + return method, path, version def is_method_allowed(self, method): """ @@ -251,7 +256,7 @@ class WebServer: ) self.http_405_html = ( "HTTP 405 - PyWebServer" - "

HTTP 404 - Method not allowed

Running PyWebServer/1.1

" + "

HTTP 405 - Method not allowed

Running PyWebServer/1.1

" "
" )