From 6bb3c64529ab99866de5db70f06d00e2c50b30a5 Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Fri, 19 Aug 2011 21:01:52 +0200 Subject: scgi: support reusing a listen socket This is useful when used in combination with e.g. systemd. --- wsgitools/scgi/asynchronous.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'wsgitools/scgi/asynchronous.py') diff --git a/wsgitools/scgi/asynchronous.py b/wsgitools/scgi/asynchronous.py index 75f1ff0..e1dbcc6 100644 --- a/wsgitools/scgi/asynchronous.py +++ b/wsgitools/scgi/asynchronous.py @@ -191,7 +191,7 @@ class SCGIServer(asyncore.dispatcher): multiple threads.""" def __init__(self, wsgiapp, port, interface="localhost", error=sys.stderr, maxrequestsize=None, maxpostsize=None, blocksize=None, - config={}): + config={}, reusesocket=None): """ @param wsgiapp: is the wsgi application to be run. @type port: int @@ -215,8 +215,16 @@ class SCGIServer(asyncore.dispatcher): @type config: {} @param config: the environ dictionary is updated using these values for each request. + @type reusesocket: None or socket.socket + @param reusesocket: If a socket is passed, do not create a socket. + Instead use given socket as listen socket. The passed socket + must be set up for accepting tcp connections (i.e. AF_INET, + SOCK_STREAM with bind and listen called). """ - asyncore.dispatcher.__init__(self) + if reusesocket is None: + asyncore.dispatcher.__init__(self) + else: + asyncore.dispatcher.__init__(self, reusesocket) self.wsgiapp = wsgiapp self.error = error @@ -229,10 +237,11 @@ class SCGIServer(asyncore.dispatcher): self.conf["blocksize"] = blocksize self.conf["config"] = config - self.create_socket(socket.AF_INET, socket.SOCK_STREAM) - self.set_reuse_addr() - self.bind((interface, port)) - self.listen(5) + if reusesocket is None: + self.create_socket(socket.AF_INET, socket.SOCK_STREAM) + self.set_reuse_addr() + self.bind((interface, port)) + self.listen(5) def handle_accept(self): """asyncore interface""" -- cgit v1.2.3