Created
March 24, 2009 07:17
-
-
Save mgax/83986 to your computer and use it in GitHub Desktop.
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 python | |
""" Serve WSGI requests via inetd / launchd """ | |
import sys | |
import socket | |
from StringIO import StringIO | |
from wsgiref.simple_server import WSGIServer, make_server | |
class InetdWSGIServer(WSGIServer): | |
""" | |
Hacked WSGIServer that handles one request when invoked from inetd | |
(tested on launchd with inetd compatibility mode) | |
""" | |
def __init__(self, server_address, RequestHandlerClass): | |
from SocketServer import BaseServer | |
BaseServer.__init__(self, server_address, RequestHandlerClass) | |
self.socket = socket.fromfd(sys.stdout.fileno(), | |
socket.AF_INET, socket.SOCK_STREAM) | |
self.server_address = self.socket.getsockname() | |
host, port = self.socket.getsockname()[:2] | |
self.server_name = socket.getfqdn(host) | |
self.server_port = port | |
self.setup_environ() | |
def get_request(self): | |
return self.socket, self.socket.getsockname()[0] | |
def serve_inetd(wsgi_app): | |
httpd = make_server(None, None, wsgi_app, server_class=InetdWSGIServer) | |
sys.stderr = StringIO() | |
httpd.handle_request() | |
if __name__ == '__main__': | |
def app(environ, start_response): | |
""" Simple application to test our server """ | |
start_response('200 OK', [('Content-type', 'text/plain;charset=utf-8')]) | |
return ["Hello inetd!\nYou are here: %s" % environ['PATH_INFO']] | |
serve_inetd(app) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment