Learning Python Network Programming

(Sean Pound) #1
Chapter 9

First the web server program needs to accept the TCP connection attempt by the
client. It then receives the HTTP request from the client over the TCP connection. The
server needs to keep the TCP connection open while it generates the HTTP response,
and it uses the connection to send the response back to the client. What the server
does with the connection after that depends on the HTTP version in use and the
value of a possible Connection header in the request (see the RFC for full details at
http://tools.ietf.org/html/rfc7230#section-6.3)..)


Once the web server has received the request, it parses it, then generates the
response. When the requested URL maps to a valid resource on the server, the server
will respond with the resource at that URL. The resource could be a file on disk
(so-called static content), as shown in the diagram of a basic HTTP request and
response from before, it could be an HTTP redirect, or it could be a dynamically
generated HTML page. If something goes wrong, or the URL is not valid, then
instead the response will include a status code in the 4xx or 5xx range. Once the
response is prepared, the server sends it back to the client over the TCP connection.


In the early days of the Web, when almost all requested resources consisted of
static files read from disk, web servers could be written in a single language and
could easily handle all four steps shown in the preceding image. However, as
more and more dynamic content came into demand, such as shopping baskets
and database-driven resources such as blogs, wikis, and social media, it was
quickly found that hard-coding these functionalities into the web server itself was
impractical. Instead, facilities were built into web servers to allow the invocation of
external code as part of the page generation process.


Hence, web servers could be written in a fast language such as C and could deal
with the low-level TCP connections, initial parsing and validating of requests,
and handling static content, but then could invoke external code to handle page
generation duties when a dynamic response was needed.

Free download pdf