A client sends an HTTP request:
GET /index.html HTTP/1.1 Host: www.example.com
The server sends back an HTTP response:
HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Content-Length: 208 <!DOCTYPE html> <html> <head> <title>Example Domain</title> </head> <body> <h1>Example Domain</h1> <p>This domain is to be used for illustrative examples in documents.</p> </body> </html>
Webpages are made up of three languages:
The most basic server just serves up HTML and multimedia files from a file system.
Server-side code is also useful for anything that requires access to persistent data or needs an additional layer of security than allowed in the client.
From the standard library, the http module can run a basic server. But it is not recommended for production.
Running a simple file server:
python -m http.server 8000
Using the http module to dynamically generate responses.
View the code by clicking the "Code" tab at https://replit.com/@PamelaFox2/SimpleServerExample
Based on this code.
An external package, Flask is a lightweight framework for server requests and responses.
Apps written in Flask:
Using the Flask framework to generate responses for each routes.
View the code by clicking the "Code" tab at https://replit.com/@PamelaFox2/SimpleFlaskExample
Based on this tutorial.
SQL can be used for 1) data storage and 2) data analysis.
View the code by clicking the "Code" tab at https://replit.com/@PamelaFox2/flask-db-example
View the code by clicking the "Code" tab at https://replit.com/@PamelaFox2/61AMerchWithTransactions
Includes a transaction:
BEGIN; UPDATE products SET quantity = quantity - 1 WHERE id = 1; INSERT INTO orders (customer, product_id) VALUES ("Animesh", 1); COMMIT;
An external library, Django is a fairly heavyweight/opinionated framework for server-side code. Includes an ORM for database interaction.
Apps written in Django:
Hog and Cats both use the http module to run a server and share common files for setting up the server.common_server.py
The Cats code includes many routes for handling multiplayer games, since those require access to the database.multiplayer.py
The Hog code includes routes for taking the next turn. It does not store anything in a database, the browser just remembers all the turns.hog_gui.py