aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2016-10-03 20:07:43 -0700
committerbnewbold <bnewbold@robocracy.org>2016-10-03 20:07:43 -0700
commit701d5bb807341e25acaf4f415bce75d08e6bc2c1 (patch)
tree619b0f2223a9d2934c680abe21ca8ea3e5e977d5 /examples
parent894772fd48cf0ede4a1ce4a90aa7694674806200 (diff)
downloadeinhyrningsins-701d5bb807341e25acaf4f415bce75d08e6bc2c1.tar.gz
einhyrningsins-701d5bb807341e25acaf4f415bce75d08e6bc2c1.zip
simple python3 http example script
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/einhorn_http.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/examples/einhorn_http.py b/examples/einhorn_http.py
new file mode 100755
index 0000000..7d93e61
--- /dev/null
+++ b/examples/einhorn_http.py
@@ -0,0 +1,40 @@
+#!/usr/bin/python3
+
+import os
+import sys
+import socket
+import socketserver
+import http.server
+
+class EinhornTCPServer(socketserver.TCPServer):
+
+ def __init__(self, server_address, RequestHandlerClass):
+ socketserver.BaseServer.__init__(self, server_address, RequestHandlerClass)
+
+ # Try to sniff first socket
+ try:
+ 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)
+
+ #self.socket = socket.fromfd(socket.AF_INET, socket.SOCK_STREAM, fd)
+ self.socket = socket.socket(fileno=fd)
+
+ try:
+ self.server_activate()
+ except:
+ self.server_close()
+ raise
+
+if __name__ == "__main__":
+ Handler = http.server.SimpleHTTPRequestHandler
+ try:
+ httpd = EinhornTCPServer(None, Handler)
+ except:
+ print("Falling back on vanilla http server on 8080")
+ httpd = socketserver.TCPServer(("localhost", 8080), Handler)
+
+ print("Serving!")
+ httpd.serve_forever()