Skip to content

Instantly share code, notes, and snippets.

@aragaer
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aragaer/c9175705f27489abb003 to your computer and use it in GitHub Desktop.
Save aragaer/c9175705f27489abb003 to your computer and use it in GitHub Desktop.
simple python low-level http server
#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, HTTPServer
class MyHandler(BaseHTTPRequestHandler):
_data_to_send = None
def _send_all(self):
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=utf-8')
self.send_header('Content-Length', str(len(self._data_to_send)))
self.end_headers()
self.wfile.write(self._data_to_send)
def do_GET(self):
result = "Hello, world\n"
self._data_to_send = result.encode('utf-8')
self._send_all()
if __name__ == '__main__':
HTTPServer(('localhost', 1080), MyHandler).serve_forever()
#!/bin/bash
./server.py &
pid=$!
finish() {
kill $pid
}
failed() {
echo FAILURE
}
trap finish EXIT
trap failed ERR
sleep 1
echo "Hello, world" | diff -u <( curl -s -S localhost:1080 ) -
echo SUCCESS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment