Learn more about Russian war crimes in Ukraine.

How to make a webserver with netcat (nc)

The netcat tool nc can operate as a TCP client. Because HTTP works over TCP, nc can be used as an HTTP server! Because nc is a UNIX tool, we can use it to make custom web servers: servers which return any HTTP headers you want, servers which return the response very slowly, servers which return invalid HTTP, etc. You can also use nc as a quick-and-dirty static file server.

Here’s an example. Run your web server by telling nc to listen for new connections on port 8000:

$ nc -l 8000

Then run your web browser. Here I use curl but you could also use Chrome etc:

$ curl localhost:8000/index.html

Back at nc, you’ll see the HTTP request come through from curl:

$ nc -l 8000
GET /index.html HTTP/1.1
Host: localhost:8000
User-Agent: curl/7.54.0
Accept: */*

nc is now waiting for you to type the response! Type out the following:

HTTP/1.1 200 Everything Is Just Fine
Server: netcat!
Content-Type: text/html; charset=UTF-8

<!doctype html>
<h1>A webpage served with netcat</h1>

Once you start typing the HTML, you’ll see it come line-by-line in your curl command. When you’ve finished typing the HTTP response, hit Ctrl-D. This tells nc to close the TCP connection and exit. The server is no more!

To run a persistent static server without typing anything in, write your HTTP response to a file like index.http:

$ cat index.http
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: netcat!

<!doctype html>
<html><body><h1>A webpage served by netcat</h1></body></html>

Then run nc in an infinite loop to serve this file for every response:

$ while true; do cat index.http | nc -l 8000; done

As an example of a “weird web server” you can make with nc, you can simulate a very slow web server. Use pv --rate-limit 10 to read the file at 10 bytes per second:

while true; do pv --rate-limit 10 index.http | nc -l 8000; done

If you view this in Chrome, you can see Chrome’s “progressive rendering”!

What can computers do? What are the limits of mathematics? And just how busy can a busy beaver be? This year, I’m writing Busy Beavers, a unique interactive book on computability theory. You and I will take a practical and modern approach to answering these questions — or at least learning why some questions are unanswerable!

It’s only $19, and you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

After months of secret toil, I and Andrew Carr released Everyday Data Science, a unique interactive online course! You’ll make the perfect glass of lemonade using Thompson sampling. You’ll lose weight with differential equations. And you might just qualify for the Olympics with a bit of statistics!

It’s $29, but you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

More by Jim

Tagged #programming, #unix, #networking. All content copyright James Fisher 2018. This post is not associated with my employer. Found an error? Edit this page.