This program sets up an HTTP server and a socket server in Python. The HTTP server serves static files and handles GET and POST requests, while the socket server receives data from clients and stores it in a JSON file.
- Python 3.x
- Poetry
- Clone the repository or download the source code files.
- Open a terminal or command prompt and navigate to the directory where the program files are located.
- Run the following command to install the project dependencies using Poetry:
poetry install
- Run the following command to start the HTTP server and the socket server:
The HTTP server will start running on
poetry run python main.py
http://localhost:3000
, and the socket server will run onlocalhost:5000
. - Open a web browser and visit
http://localhost:3000
to access the web application.
The program consists of two main components: the HTTP server and the socket server.
The HTTP server handles GET and POST requests and serves static files.
-
GET requests:
- Requests to the root URL (
/
) will display theindex.html
file. - Requests to the
/message
URL will display themessage.html
file. - Requests to other valid static file paths will serve the corresponding file.
- Requests to the root URL (
-
POST requests:
- When a POST request is made to the
/message
URL, the server receives the form data, extracts the key-value pairs, and stores them in a dictionary with the current timestamp as the key. - The server then sends the dictionary to the socket server using UDP.
- After processing the data, the user is redirected to the root URL (
/
).
- When a POST request is made to the
The socket server receives data sent by the HTTP server, parses it as JSON, and stores it in a JSON file.
- The socket server listens on
localhost:5000
for incoming UDP data. - The received data is decoded and parsed as a JSON object.
- The parsed data is merged with the existing data stored in the
storage/data.json
file. - The updated data is then saved back to the JSON file.
- The HTTP server is set to run on
http://localhost:3000
by default. You can change the server address by modifying theserver_address
variable in therun_http_server
function. - The socket server is set to run on
localhost:5000
by default. You can change the server address by modifying theserver_address
variable in therun_socket_server
function. - The storage path for the JSON file is set to
storage/data.json
by default. You can modify thefile_path
variable in thesave_to_json_file
function to change the storage location.
This program is licensed under the MIT License.
This program is inspired by the basic functionality of HTTP and socket servers in Python.