Webserver Tricks#
When the operator needs an HTTP listener and the box has no nginx, no Apache, no installed web framework, a rudimentary webserver written in whatever interpreter is on hand will do. The handbook collects one-shot servers across AWK, Go, JavaScript, Perl, PHP, Python, and a plain UNIX shell loop.
AWK#
#!/usr/bin/gawk -f
BEGIN {
RS = ORS = "\r\n"
HttpService = "/inet/tcp/8080/0/0"
Hello = "<HTML><HEAD>" \
"<TITLE>A Famous Greeting</TITLE></HEAD>" \
"<BODY><H1>Hello, world</H1></BODY></HTML>"
Len = length(Hello) + length(ORS)
print "HTTP/1.0 200 OK" |& HttpService
print "Content-Length: " Len ORS |& HttpService
print Hello |& HttpService
while ((HttpService |& getline) > 0)
continue;
close(HttpService)
}
Go#
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "Goodbye, World!")
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
JavaScript#
Node.js.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Goodbye, World!\n');
}).listen(8080, '127.0.0.1');
Perl#
use Socket;
my $port = 8080;
my $protocol = getprotobyname("tcp");
socket(SOCK, PF_INET, SOCK_STREAM, $protocol) or die "couldn't open a socket: $!";
setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR, 1) or die "couldn't set socket options: $!";
bind(SOCK, sockaddr_in($port, INADDR_ANY)) or die "couldn't bind socket to port $port: $!";
listen(SOCK, SOMAXCONN) or die "couldn't listen to port $port: $!";
while(accept(CLIENT, SOCK)){
print CLIENT "HTTP/1.1 200 OK\r\n" .
"Content-Type: text/html; charset=UTF-8\r\n\r\n" .
"<html><head><title>Goodbye, world!</title></head><body>Goodbye, world!</body></html>\r\n";
close CLIENT;
}
PHP#
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die('Failed to create socket!');
socket_bind($socket, 0, 8080);
socket_listen($socket);
$msg = '<html><head><title>Goodbye, world!</title></head><body>Goodbye, world!</body></html>';
for (;;) {
if ($client = @socket_accept($socket)) {
socket_write($client, "HTTP/1.1 200 OK\r\n" .
"Content-length: " . strlen($msg) . "\r\n" .
"Content-Type: text/html; charset=UTF-8\r\n\r\n" .
$msg);
} else usleep(100000);
}
?>
Python#
Python under 3.2 with wsgiref.simple_server.
from wsgiref.simple_server import make_server
def app(environ, start_response):
start_response('200 OK', [('Content-Type','text/html')])
yield b"<h1>Goodbye, World!</h1>"
server = make_server('127.0.0.1', 8080, app)
server.serve_forever()
Python 3 with http.server.
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
class HelloHTTPRequestHandler(BaseHTTPRequestHandler):
message = 'Hello World!'
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=UTF-8')
self.end_headers()
self.wfile.write(self.message.encode('utf-8'))
self.close_connection = True
def serve(addr, port):
with ThreadingHTTPServer((addr, port), HelloHTTPRequestHandler) as server:
server.serve_forever(poll_interval=None)
if __name__ == '__main__':
addr, port = ('localhost', 80)
threading.Thread(target=serve, args=(addr, port), daemon=True).start()
UNIX shell#
$ while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; echo 'Hello, World!'; } | nc -l 8080; done