VSPEXHOST Documentation
Overview

VSPEXHOST is a simple Python web server that provides an easy-to-understand implementation for handling HTTP requests, rendering HTML responses, and serving logs through a GUI. It supports routing, logging, and a customizable web server experience.

Installation
# Install the necessary libraries (Python 3.x required)
pip install tkinter
pip install threading
            
Main Components

The main components of VSPEXHOST are:

Router Class

The Router class manages all routes and methods for the server. It maps paths to specific functions that will handle HTTP requests.

class Router:
    def __init__(self):
        self.routes = {}
        self.methods = {}

    def setpath(self, client, path, methods=["GET"]):
        def wrapper(func):
            self.routes[path] = func
            self.methods[path] = methods
            return func
        return wrapper

    def get_route(self, path):
        return self.routes.get(path, None)

    def get_methods(self, path):
        return self.methods.get(path, ["GET"])
            

The setpath method is used to associate a function with a specific route. The get_route and get_methods methods retrieve the route and allowed HTTP methods for a given path.

VSPEXHOST Class

The VSPEXHOST class is the core of the application, handling server initialization, logging, and routing of requests.

class VSPEXHOST:
    def __init__(self, host='localhost', port=8080):
        self.host = host
        self.port = port
        self.router = Router()
        self.logs = {"Errors": [], "Messages": [], "Requests": [], "Welcome": []}
        self.gui_initialized = False

    def log_action(self, section, action):
        timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        log_entry = f"{action} - {timestamp}"
        self.logs[section].append(log_entry)
        if self.gui_initialized:
            self.update_gui()

    def make(self, render, html, code, client, application):
        if render == "html":
            return self.render_html(code)
        return code

    def render_html(self, content):
        return f"{content}"

    def handle_request(self, client_socket):
        request_data = client_socket.recv(1024).decode('utf-8')
        if request_data:
            first_line = request_data.split("\n")[0]
            path = first_line.split(" ")[1]
            method = first_line.split(" ")[0]
            self.log_action("Requests", f"Received {method} request for {path}")
            route_func = self.router.get_route(path)
            allowed_methods = self.router.get_methods(path)
            if route_func and method in allowed_methods:
                response_code = 'HTTP/1.1 200 OK'
                content = route_func()
                self.log_action("Requests", f"Route {path} found, responding with 200 OK")
            else:
                response_code = 'HTTP/1.1 404 Not Found'
                content = self.make("render", "html", "

404 Not Found

", client_socket, self) self.log_action("Errors", f"Route {path} not found, responding with 404 Not Found") response = f"{response_code}\r\nContent-Type: text/html\r\n\r\n{content}" client_socket.sendall(response.encode('utf-8')) client_socket.close()

This class initializes the server, logs actions, and renders responses for incoming HTTP requests.

Logging and GUI

VSPEXHOST supports logging different actions to keep track of requests, errors, and server messages. The GUI is built using tkinter and displays the logs in real-time.

def initialize_gui(self):
    self.root = tk.Tk()
    self.root.title("VSPEXHOST Server Logs")
    self.textbox = tk.Text(self.root, height=20, width=80)
    self.textbox.pack()
    self.gui_initialized = True

def update_gui(self):
    if self.gui_initialized:
        self.textbox.delete(1.0, tk.END)
        for section, logs in self.logs.items():
            self.textbox.insert(tk.END, f"\n{section}:\n")
            for log in logs:
                self.textbox.insert(tk.END, log + '\n')
        self.root.update_idletasks()
        self.root.update()
            

The GUI is initialized with the initialize_gui method and updates the log display with the update_gui method.

Running the Server

The server can be started by calling the start method, which listens for incoming connections and handles requests in separate threads.

if __name__ == '__main__':
    app = VSPEXHOST(host='localhost', port=8081)
    @app.router.setpath(client=None, path='/')
    def home():
        return app.make("render", "html", "

Welcome to VSPEXHOST!

This is the home page.

", None, app) app.start()
Full Python Code
import socket
import threading
import tkinter as tk
from datetime import datetime

class Router:
    def __init__(self):
        self.routes = {}
        self.methods = {}

    def setpath(self, client, path, methods=["GET"]):
        def wrapper(func):
            self.routes[path] = func
            self.methods[path] = methods
            return func
        return wrapper

    def get_route(self, path):
        return self.routes.get(path, None)

    def get_methods(self, path):
        return self.methods.get(path, ["GET"])

class VSPEXHOST:
    def __init__(self, host='localhost', port=8080):
        self.host = host
        self.port = port
        self.router = Router()
        self.logs = {"Errors": [], "Messages": [], "Requests": [], "Welcome": []}
        self.gui_initialized = False

    def log_action(self, section, action):
        timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        log_entry = f"{action} - {timestamp}"
        self.logs[section].append(log_entry)
        if self.gui_initialized:
            self.update_gui()

    def make(self, render, html, code, client, application):
        if render == "html":
            return self.render_html(code)
        return code

    def render_html(self, content):
        return f"{content}"

    def handle_request(self, client_socket):
        request_data = client_socket.recv(1024).decode('utf-8')
        if request_data:
            first_line = request_data.split("\n")[0]
            path = first_line.split(" ")[1]
            method = first_line.split(" ")[0]
            self.log_action("Requests", f"Received {method} request for {path}")
            route_func = self.router.get_route(path)
            allowed_methods = self.router.get_methods(path)
            if route_func and method in allowed_methods:
                response_code = 'HTTP/1.1 200 OK'
                content = route_func()
                self.log_action("Requests", f"Route {path} found, responding with 200 OK")
            else:
                response_code = 'HTTP/1.1 404 Not Found'
                content = self.make("render", "html", "

404 Not Found

", client_socket, self) self.log_action("Errors", f"Route {path} not found, responding with 404 Not Found") response = f"{response_code}\r\nContent-Type: text/html\r\n\r\n{content}" client_socket.sendall(response.encode('utf-8')) client_socket.close()