Newby Coder header banner

Python Http Server

What is an HTTP Server?

An HTTP web server is a process that runs on a machine and :

  1. Listens for incoming http requests on a specific TCP socket address (IP address and a port number)

  2. Handles this request and sends a response back to user

The TCP Socket Address

In order to reach its destination, each http message carries an address called destination TCP address

Each TCP address is composed of an IP address and a port number

The web server is a regular network application that is listens continuously on a specific port and http request must be addressed to that port

By default, port number is 80 for http and 443 for https

Basic Python Http Server without additional code

A web server can be started with python without having to write any scripts, with the command

$ python -m http.server 8080 

By default, this server is configured to listen on all interfaces and on port 8080

To listen to a specific interface bind parameter can be used :

$ python -m http.server 8080 --bind 127.0.0.1 

Also from Python 3.7, –directory flag can be used to serve files from a directory that is not necessarily current directory

When started, it searches an index.html file in the current working directory, which is served as the initial page

If index.html is not found, then it acts as a File Explorer and displays the files and directories of current working directory

The contents of the files can then be viewed from a Browser

Create a simple HTML file

<html>
    <head>
        <title>Python!</title>
    </head>
    <body>
        <h1>NewbyCoder.com</h1>
        <p>HTTP Server is working</p>
    </body>
</html> 

Save this file as index.html

Next step is to create a web server that serves this html page

Basic Python Web Server

To create a web server in Python 3, following two modules have to be imported : http.server(SimpleHTTPServer in Python 2) and socketserver

Following code creates an http server:

import http.server
import socketserver

PORT = 8080
handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever() 

A web server has handlers to handle incoming requests

A web server can be thought of as a dispatcher, which inspects any incoming request and dispatches it to a designated handler

A basic handler just serves a static file

http.server.SimpleHTTPRequestHandler is a simple HTTP request handler that serves files from current directory and any of its subdirectories

An instance of TCPServer denotes a server that uses TCP protocol to send and receive messages (http is an application layer protocol on top of TCP)

socketserver.TCPServer(("", PORT), Handler) 

Instantiation of a TCP Server requires :

TCP address is passed as a tuple of (ip address, port number)

Passing an empty string as ip address means that server should be listening on any network interface (all available IP addresses)

The server listens on incoming requests on specified port (8080 above)

For handler, simple handler is passed

Handler = http.server.SimpleHTTPRequestHandler 

serve_forever is a method of TCPServer instance that starts server and begins listening and responding to incoming requests

Code provided above, is saved as server.py in the same directory as index.html because by default SimpleHTTPRequestHandler looks for a file named index.html in current directory

Web server can be started in that directory as:

$ python server.py
serving at port 8080 

This starts an HTTP server that listens on any interface (wifi, lan) at port 8080 waiting for incoming http requests

When localhost:8080 is visited in a browser, it displays index.html

localhost

localhost is used to access network services that are running on host

localhost can be replaced with 127.0.0.1 in browser to get the same result

$ host localhost
localhost has address 127.0.0.1
localhost has IPv6 address ::1