X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=python%2Fscgi-wsgi;fp=python%2Fscgi-wsgi;h=944ed3996aa08ec1b8f46c4ea60c040d14019796;hb=c06db49a3a4bfbf14b1661b667e1ed1cbab2bcd0;hp=0000000000000000000000000000000000000000;hpb=608f4ac7a840277f9754d8fe0410a31727057d3f;p=ashd.git diff --git a/python/scgi-wsgi b/python/scgi-wsgi new file mode 100755 index 0000000..944ed39 --- /dev/null +++ b/python/scgi-wsgi @@ -0,0 +1,58 @@ +#!/usr/bin/python + +import sys, os, getopt +import socket +import ashd.scgi + +def usage(out): + out.write("usage: scgi-wsgi [-hA] [-p MODPATH] [-T [HOST:]PORT] HANDLER-MODULE [ARGS...]\n") + +sk = None +modwsgi_compat = False +opts, args = getopt.getopt(sys.argv[1:], "+hAp:T:") +for o, a in opts: + if o == "-h": + usage(sys.stdout) + sys.exit(0) + elif o == "-p": + sys.path.append(0, a) + elif o == "-T": + sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + p = a.rfind(":") + if p < 0: + bindhost = "hostname" + bindport = int(a) + else: + bindhost = a[:p] + bindport = int(a[p + 1:]) + sk.bind((bindhost, bindport)) + sk.listen(32) + elif o == "-A": + modwsgi_compat = True +if len(args) < 1: + usage(sys.stderr) + sys.exit(1) + +if sk is None: + # This is suboptimal, since the socket on stdin is not necessarily + # AF_UNIX, but Python does not seem to offer any way around it, + # that I can find. + sk = socket.fromfd(0, socket.AF_UNIX, socket.SOCK_STREAM) + +try: + handlermod = __import__(args[0], fromlist = ["dummy"]) +except ImportError, exc: + sys.stderr.write("scgi-wsgi: handler %s not found: %s\n" % (args[0], exc.message)) + sys.exit(1) +if not modwsgi_compat: + if not hasattr(handlermod, "wmain"): + sys.stderr.write("scgi-wsgi: handler %s has no `wmain' function\n" % args[0]) + sys.exit(1) + handler = handlermod.wmain(args[1:]) +else: + if not hasattr(handlermod, "application"): + sys.stderr.write("scgi-wsgi: handler %s has no `application' object\n" % args[0]) + sys.exit(1) + handler = handlermod.application + +ashd.scgi.servescgi(sk, ashd.scgi.wrapwsgi(handler))