nc one-liner Linux local test server
Run a local test server that prints requests and does not reply with the Ubuntu pre-installed nc utility:
nc -kdl 8000
To actually send a minimal empty HTTP reply back in order to unblock HTTP clients such as wget that wait for a reply so you can do more tests afterwards on the other shell without manually interrupting the client (thanks to nikniknik and zelanix in the comments):
while true; do printf 'HTTP/1.1 200 OK\r\n\r\n' | nc -Nl 8000; done
And if you want to get real fancy and actually send the date on the reply body:
while true; do
resp=$"$(date): hello\n"
len="$(printf '%s' "$resp" | wc -c)"
printf "HTTP/1.1 200 OK\r\nContent-Length: $len\r\n\r\n${resp}\n" | nc -Nl 8000
done
Sample request maker on another shell:
curl -vvv localhost:8000
then on the shell with the server you see the request showed up:
GET / HTTP/1.1
Host: localhost:8000
User-Agent: curl/8.2.1
Accept: */*
And on the server version where we return the date, on the request maker shell we can see a reply like:
Thu Dec 14 12:08:33 PM GMT 2023: hello
nc from the netcat-openbsd package is widely available and pre-installed on most Ubuntu.
For even more fun, this is a fine IasS hello world test of your favorite provider, e.g. I've recently done this from AWS EC2 Ubuntu image and it worked well after port 80 was opened in the security settings: Opening port 80 EC2 Amazon web services
Related questions about the minimal value HTTP reply:
Related:
Tested on Ubuntu 22.10, nc from netcat-openbsd 1.218.
python -m http.server local file server
This one is also handy, it serves files from the current working directory, so it gives you a simple way to setup different answers to different requests.
Create two test files:
echo aaa > bbb
echo 000 > 111
Runt the server on the default port 8000:
python -m http.server
Query one of the paths:
curl -vvv http://localhost:8000/bbb
Output:
* Host localhost:8000 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying [::1]:8000...
* connect to ::1 port 8000 from ::1 port 33928 failed: Connection refused
* Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000
> GET /bbb HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/8.5.0
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Server: SimpleHTTP/0.6 Python/3.12.3
< Date: Wed, 13 Nov 2024 07:44:23 GMT
< Content-type: application/octet-stream
< Content-Length: 4
< Last-Modified: Wed, 13 Nov 2024 07:43:43 GMT
<
aaa
* Closing connection