Skip to content

Tiny Server

You'll write a server program of your own that builds a page at the moment someone asks for it, then load it on your phone from across the room.

About

Every page so far was a file that sat still until a browser fetched it. A server program is different: it builds the page when the request comes in, so it can show the time, count visitors or answer a form. You'll write one in whatever language is already on your machine, like Python, Node, PHP, Ruby or Go, in well under 100 lines and without a framework, because the point is to watch a request arrive and a response leave. It runs on your own laptop for now and costs nothing but a weekend. If you'd rather learn the language itself, that's Coding next door. Here the output is still a page someone can visit.

At a glance

Per requirement
25 XP
Completion bonus
150 XP
Requirements
6
Takes
One weekend
Skill level
Intermediate (months of practice)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own
Locked for now
Earn two more Common badges in Web Building to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Run a server that answers on port 8000. Use the standard library of whatever you have: Python's http.server, Node's http module, PHP's built-in server or Go's net/http. Write a handler that returns a short HTML page with a heading, start it, and open http://localhost:8000 in your browser. Take a screenshot with the address bar and the page both showing. Photo
  2. 2
    Print one raw request to the terminal and read it. Log the request line and the headers as they arrive, then load the page once. You'll see the method, the path, the User-Agent and any cookies, which is everything the browser actually sent and a lot less mysterious than it sounds. Take a screenshot of the terminal with one full request printed. Photo
  3. 3
    Put the current time in the page and reload it. Build the HTML as a string on the server with the time written into it, so every reload shows a new second. Then view source in the browser and notice there's no code in it, only the result, which is the whole difference between a server and a file. Take a screenshot of two reloads side by side with different times. Photo
  4. 4
    Accept a form POST and echo it back with the angle brackets escaped. Add a form with one text field that posts to your server, read the body, and return a page that repeats what was typed. Escape < and > before you print it, because the first thing anyone types into a new form is a tag, and anything you print unescaped runs in your visitors' browsers. Film yourself typing a bold tag into the form and watching it come back as plain text. Video
  5. 5
    Keep a visit counter in a file on disk. Read a number from a file, add one, write it back and show it on the page. Restart the server and the count should carry on from where it was, which is the first time your site has remembered anything. Take a screenshot with the terminal showing the restart and the page showing the count carrying on. Photo
  6. 6
    Load it on your phone over the same Wi-Fi. Bind the server to 0.0.0.0 instead of localhost, find your laptop's address on the local network with ipconfig or ifconfig, and type it into your phone with the port. If it won't load, the laptop's firewall is usually why. Stop the server when you're done, because a program listening on every interface is a door you've opened. Take a photo of the phone showing the page with the laptop in the background. Photo