diff options
author | bnewbold <bnewbold@robocracy.org> | 2016-10-09 16:32:02 -0700 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2016-10-09 16:32:02 -0700 |
commit | 89f2f02ab813fe19fd029e95f6a2f6ce65c5a16b (patch) | |
tree | fbecf2aa0797888f2ded12b704eb9762743fb00a /examples | |
parent | 5a84c5287040d57b31b6b6d63f9dc938915e80a2 (diff) | |
download | einhyrningsins-89f2f02ab813fe19fd029e95f6a2f6ce65c5a16b.tar.gz einhyrningsins-89f2f02ab813fe19fd029e95f6a2f6ce65c5a16b.zip |
upate python example with KeyboardInterrupt catching
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/einhorn_http.py | 24 |
1 files changed, 18 insertions, 6 deletions
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() |