Back to the client

Remember, our client couldn't do anything because we didn't have a server running. Now we do! Open a new Terminal window (on macOS hit Command + N, on Windows just run another cmd), and repeat the same command:

nc 127.0.0.1 8080

This time the nc program will not quit and the cursor will move to a new line. Now the connection is established, we have a portal running! It's time to write that GET request. Type it:

GET /article.html HTTP/1.1
Host: coolsite.com

You have to hit Enter after HTTP/1.1. Then, after the second line, hit Enter two more times. You should get a response from the server similar to this:

HTTP/1.0 404 File not found
Server: SimpleHTTP/0.6 Python/3.7.1
Date: Mon, 26 Nov 2018 20:44:56 GMT
Connection: close
Content-Type: text/html;charset=utf-8
Content-Length: 469

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <title>Error response</title>
    </head>
    <body>
        <h1>Error response</h1>
        <p>Error code: 404</p>
        <p>Message: File not found.</p>
        <p>Error code explanation: HTTPStatus.NOT_FOUND - Nothing matches the given URI.</p>
    </body>
</html>

Woah! Read the first line and you should get an idea of what's happened: the thing you've requested is not found. Do you remember creating a file "article.html"? Me neither. Our server runs fine, but a file with that name doesn't exist.

The server we've built has access to the same folder it's located in, and it "serves" the files from it. When the request asks for "article.html", the server looks into the folder and tries to find the requested file. Let's create that file and save it next to server.py. Go to VS Code and create a new file, put the following code in it:

<!doctype html>
<html lang=en>
  <body>
    <h1>Article title</h1>
    <p>Some very important text.</p>
  </body>
</html>

Once again, we're jumping ahead and writing some HTML without understanding what it means. We'll talk about HTML in the future, of course. For now, just save that file as article.html in the same folder where your server is located.

Now repeat the GET request.

This is what you should get back from the server:

HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.7.1
Date: Mon, 26 Nov 2018 21:07:37 GMT
Content-type: text/html
Content-Length: 122
Last-Modified: Mon, 26 Nov 2018 21:07:25 GMT

<!doctype html>
<html lang=en>
  <body>
    <h1>Article title</h1>
    <p>Some very important text.</p>
  </body>
</html>

Yes! Even though it says "HTTP/1.0" instead of "1.1", the important part is that the request was successful and we get a "200 OK" status code. This means everything is okay. After a few lines of technical information (these are called "headers"), we see the contents of that HTML file.

If this was your browser, and not nc, at this point it would've saved that HTML file somewhere and opened for you to see.

Well, that's it! The fundamental layer is ready to be built upon! The next steps are to learn about further layers to explore the wonderfully complex world of modern web development.