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.
simpleserverexample.pamelafox2.repl.co/path
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.
simpleflaskexample.pamelafox2.repl.co/
View the code by clicking the "Code" tab at https://replit.com/@PamelaFox2/SimpleFlaskExample
Based on this tutorial.
Web apps store any data in databases that needs to be shared across multiple users/computers.
flaskdbexample-orm.pamelafox2.repl.co/
View the code by clicking the "Code" tab at https://replit.com/@PamelaFox2/FlaskDBExample-ORM
61amerch-orm.pamelafox2.repl.co/
View the code by clicking the "Code" tab at https://replit.com/@PamelaFox2/61aMerch-ORM
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.pyThe Cats code includes many routes for handling multiplayer games, since those require access to the database.
multiplayer.pyThe 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