Last active
August 29, 2015 14:24
-
-
Save aragaer/c9175705f27489abb003 to your computer and use it in GitHub Desktop.
simple python low-level http server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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