An open API service indexing awesome lists of open source software.

https://github.com/endtoenddev/democracy-drive

This code creates a Tkinter GUI to manage a web server. Users can submit their name and answer, which are stored in an XML file. The server runs in a separate thread, automatically shuts down when a timer ends, and displays server status. The GUI allows setting the question, host, and timer, with controls to start/stop the server.
https://github.com/endtoenddev/democracy-drive

clientside css html js python serverside

Last synced: about 2 months ago
JSON representation

This code creates a Tkinter GUI to manage a web server. Users can submit their name and answer, which are stored in an XML file. The server runs in a separate thread, automatically shuts down when a timer ends, and displays server status. The GUI allows setting the question, host, and timer, with controls to start/stop the server.

Awesome Lists containing this project

README

        

This Python code is a web server that integrates with a GUI using `customtkinter` and `tkinter` to facilitate interactive sessions with users. The main functionality revolves around running a web server that serves a form-based page where users can submit answers to a question. READ "IMPORTANT INFORMATION FOR DOWNLOAD" AND "RUNNING main.py". Below is a summary of the code's components:

### 1. **IMPORTANT INFORMATION FOR DOWNLOAD**
- I am working on a video tutorial but for the meantime here are the steps!
- Go to python.org and download the latest version
- Download the latest release and extract it in any folder you want it to live.
- If you don't want to type your ip in everytime follow these steps.
- For Windows - open cmd
- Do `ipconfig`
- Find `ipv4 address : "YOUR IP ADDRESS"`
- Remember this for later.
- Open main.py in any text editor.
- Locate the start_server(port, host, etc...) line.
- Change `host="192.168.1.80"` to `host="YOUR IP ADDRESS"`
- Then locate line 427 or `host_label = ctk.CTkLabel(root, text="Enter host (optional, default: 192.168.1.80):")`
- Change this into `host_label = ctk.CTkLabel(root, text="Enter host (optional, default: YOUR IP ADDRESS THAT YOU PUT IN HOST):")`
- Important to mention put your actual ip address not "YOUR IP ADDRESS" because if you do it won't work.
- That's it for setting up your Ip
- Navigate to your folder for this directory in command prompt
- Do `pip install customtkinter`
- That's it for setup

### 2. **RUNNING main.py**
- MUST BE ADMIN
- And run main.py by doing `python3 main.py` it will check if you are admin.
- It should run perfectly if you correctly modified the code as you should have in "IMPORTANT INFORMATION FOR DOWNLOAD".

### 3. **Imports and Dependencies**
- The script imports a variety of libraries such as `random`, `customtkinter`, `http.server`, `xml.etree.ElementTree`, and others.
- `customtkinter` (CTk) is used for creating a modern styled GUI, while `http.server` handles the HTTP server functionality.
- The code interacts with XML files, performs file operations, and supports a web-based front-end.

### 4. **Global Variables and Functions**
- **`generate_random_port()`**: This function generates a random port number between 8000 and 8080 for the server to run on.
- **`today` and `formated_today`**: These variables capture today's date and format it for later use in storing form responses.
- **`xml_lock`**: A threading lock to safely access and modify XML data in a multi-threaded environment.

### 5. **Window and File Handling Functions**
- **`open_destroy_window()`** and **`open_read_window()`**: These functions trigger the `file()` and `code()` functions from external modules to manage specific window functionalities like file destruction and reading.
- **`append_to_xml()`**: This function appends user responses (name and answer) to an XML file, ensuring it’s well-formed and formatted.

### 6. **HTTP Server Setup**
- **HTML Content**: The server serves an HTML page that includes a form where users can enter their name and answer to a question. The page also displays a timer, word count, and the server's IP and port.
- **`normUserHTTP`**: This class extends `BaseHTTPRequestHandler` to define how GET and POST requests are handled:
- **GET**: Serves the HTML page and static images (like flags).
- **POST**: Receives form data (name and answer), appends it to the XML file, and re-renders the page.

### 7. **Server Functions**
- **`start_server()`**: This function sets up the HTTP server to listen on a dynamically generated port. It also handles form submissions by appending responses to an XML file and updating the webpage. It runs in a separate thread, allowing the GUI to stay responsive.
- **`update_timer()`**: This function counts down the specified time and shuts down the server when the time expires.

### 8. **GUI Functions**
- **`start_gui()`**: This function creates and runs a `customtkinter` GUI. It allows users to:
- Enter a question to be asked via the web server.
- Set the host address and timer duration.
- Start or stop the server with buttons.
- Display the current server status, IP, and port.
- Trigger actions for opening the destroy or read windows.

- **Start and Stop Buttons**: The server can be started with the "Start Server" button, and it runs in a separate thread. The "Stop Server" button gracefully stops the server by setting the `stop_event` flag.

### 9. **HTML and JavaScript for User Interaction**
- The served HTML page includes input fields for the user to submit their name and response. It uses JavaScript to:
- Store the user's input using AJAX (via `fetch`).
- Dynamically resize the input field and update the word count.
- Display the remaining time on the page.

### 10. **Threading and Concurrency**
- **Threads**: The server is run in a separate thread, allowing the GUI to remain interactive. The timer is also run in a separate thread to handle countdowns independently of the main server loop.
- **Events**: `stop_event` and `shutdown_event` are used to control the server’s lifecycle and shut it down when necessary.

### 11. **Error Handling**
- The code includes error handling for various scenarios, such as missing files or server startup issues. If an error occurs, a message box is shown to inform the user.

### 12. **Main Function**
- The `start_gui()` function is invoked when the script is run, which sets up the GUI and starts the interactive server process.

### Summary:
The code defines a web-based interactive system where users can answer questions within a time limit. It serves a page with a question and inputs for the user's name and response. Upon submission, the answers are saved in an XML file. The system integrates a modern GUI using `customtkinter` and allows for real-time updates to the user interface, including a countdown timer, word count display, and server status. The server is designed to be started and stopped via the GUI.

"guy who made this" notes Made by “EndToEndDev”
Total Updates (so far) : 288
Last Update Time : 8:42 A.m. 11/16/2024