From 89f2f02ab813fe19fd029e95f6a2f6ce65c5a16b Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sun, 9 Oct 2016 16:32:02 -0700 Subject: upate python example with KeyboardInterrupt catching --- examples/einhorn_http.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'examples') diff --git a/examples/einhorn_http.py b/examples/einhorn_http.py index 7d93e61..304d01a 100755 --- a/examples/einhorn_http.py +++ b/examples/einhorn_http.py @@ -1,4 +1,11 @@ -#!/usr/bin/python3 +#!/usr/bin/env python3 +""" +This small example program demonstrates one way to integerate with Einhorn using +Python (3). + +It serves up the current working directory over HTTP on either the +Einhorn-supplied socket or localhost:8080. +""" import os import sys @@ -16,11 +23,11 @@ class EinhornTCPServer(socketserver.TCPServer): fd = int(os.environ['EINHORN_FD_0']) print("Will try to listen with fd=%d" % fd) except KeyError: - print("Couldn't find EINHORN_FD_0 env variable... is this running under einhorn?") - sys.exit(1) + raise EnvironmentError("Couldn't find EINHORN_FD_0 env variable... is this running under einhorn?") - #self.socket = socket.fromfd(socket.AF_INET, socket.SOCK_STREAM, fd) self.socket = socket.socket(fileno=fd) + # alternative? + #self.socket = socket.fromfd(socket.AF_INET, socket.SOCK_STREAM, fd) try: self.server_activate() @@ -32,9 +39,14 @@ if __name__ == "__main__": Handler = http.server.SimpleHTTPRequestHandler try: httpd = EinhornTCPServer(None, Handler) - except: + except EnvironmentError as ee: + print(ee) print("Falling back on vanilla http server on 8080") httpd = socketserver.TCPServer(("localhost", 8080), Handler) print("Serving!") - httpd.serve_forever() + try: + httpd.serve_forever() + except KeyboardInterrupt: + print("Caught KeyboardInterrupt, shutting down") + httpd.server_close() -- cgit v1.2.3