DIY web server

Here comes the first challenge. On one hand, you want to make something that works as soon as possible, prove yourself you can really create a computer application from nothing. On the other hand, even a simple HTTP server, which consists of just 4 lines of code, is a pretty complex program. Each line includes tens and tens of different concepts.

One way to go — a classic, "proper" way — is to start with the basics of Python and make our way up the ladder of difficulty. We'd need to learn about variables, modules, importing, TCP, handlers, with-statements and command line arguments. It's quite a list, and each concept requires you to at least gave some idea about a dozen more. It'll take us a few days or even weeks!

The rabbit's hole is very, very deep.

Another way to go is to just give you those 4 lines to copy and paste and hope it works. You'll have no idea what's happening, but the HTTP server will probably be launched successfully.

I believe there's a third way. I will indeed give you 5 lines of code to copy and paste. I will also describe them, but won't go deep. But this is a loan, not a gift. You'll have to pay back!

In the future courses and books we will look back and cover the missing parts. I call this a "learning credit card". We'll always jump ahead and do something cool, but then at some point we'll come back and "pay the debt". As long as we're disciplines and don't take too many "knowledge loans", we'll be fine and it'll be fun!

So, here comes the first loan :-) Go to your code editor (VS Code), click File -> New File and create a new file called server.py. It doesn't matter where, but it'll be easier if you create a folder somewhere first. Call the folder as you wish, e.g. "webserver".

Copy and paste the following code into the file:

import http.server
import socketserver

with socketserver.TCPServer(("", 8080), http.server.SimpleHTTPRequestHandler) as httpd:
    httpd.serve_forever()

This is how it looks like in my VS Code:

The first two lines import some code from two other files that come build-in with Python itself. This term is used in many programming languages and it means what you think it means: some useful code is stored somewhere else, and you want to "get" it here, in your file. import tells the Python interpreter to "take" it, essentially, copy and paste into your program.

The final two lines use some that imported code to create a new server, specify its port 8080 and makes sure that requests are processed with SimpleHTTPRequestHandler: a special function that comes with Python which knows how to accept requests and create responses.

Save the file, then go back to the terminal and navigate to that folder.

On macOS: the easiest way is to enter cd in the Terminal, hit Spacebar and then drag-n-drop the folder into the Terminal. This will save you from typing the full path by hand.

But if you prefer, you can do it by hand. For example, if your folder is called server and is located on your desktop, then type cd /Users/YOUR_USERNAME_HERE/Desktop/server. Don't forget to change YOUR_USERNAME_HERE with your actual username.

(cd stands for "change directory", by the way).

On Windows: assuming your folder is saved on the Desktop, in the Terminal type cd C:\Users\YOUR_USERNAME_HERE\Desktop\server. Don't forget to change YOUR_USERNAME_HERE with your actual username.

Now run the server by typing:

  • On macOS: python3 server.py
  • On Windows: python server.py

(Windows might ask you to "Allow access" since your web server is a new application that tries to use the network; press "Allow access").

If everything is okay, the Terminal will kind of freeze and stop on a new line.