3 import sys, os, getopt, logging
5 import ashd.scgi, ashd.perf, ashd.serve
12 out.write("usage: scgi-wsgi [-hAL] [-m PDM-SPEC] [-p MODPATH] [-T [HOST:]PORT] HANDLER-MODULE [ARGS...]\n")
15 modwsgi_compat = False
17 opts, args = getopt.getopt(sys.argv[1:], "+hALp:T:m:")
27 sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
28 sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
31 bindhost = "localhost"
35 bindport = int(a[p + 1:])
36 sk.bind((bindhost, bindport))
47 logging.basicConfig(format="scgi-wsgi(%(name)s): %(levelname)s: %(message)s")
50 # This is suboptimal, since the socket on stdin is not necessarily
51 # AF_UNIX, but Python does not seem to offer any way around it,
53 sk = socket.fromfd(0, socket.AF_UNIX, socket.SOCK_STREAM)
56 handlermod = __import__(args[0], fromlist = ["dummy"])
57 except ImportError, exc:
58 sys.stderr.write("scgi-wsgi: handler %s not found: %s\n" % (args[0], exc.message))
60 if not modwsgi_compat:
61 if not hasattr(handlermod, "wmain"):
62 sys.stderr.write("scgi-wsgi: handler %s has no `wmain' function\n" % args[0])
64 handler = handlermod.wmain(*args[1:])
66 if not hasattr(handlermod, "application"):
67 sys.stderr.write("scgi-wsgi: handler %s has no `application' object\n" % args[0])
69 handler = handlermod.application
73 env["wsgi.version"] = 1, 0
74 if "HTTP_X_ASH_PROTOCOL" in env:
75 env["wsgi.url_scheme"] = env["HTTP_X_ASH_PROTOCOL"]
77 env["wsgi.url_scheme"] = "https"
79 env["wsgi.url_scheme"] = "http"
80 env["wsgi.input"] = sk
81 env["wsgi.errors"] = sys.stderr
82 env["wsgi.multithread"] = True
83 env["wsgi.multiprocess"] = False
84 env["wsgi.run_once"] = False
87 class reqthread(ashd.serve.wsgithread):
88 def __init__(self, sk):
89 super(reqthread, self).__init__()
91 self.sk = self.bsk.makefile("r+")
94 return handler(self.env, self.startreq)
96 def writehead(self, status, headers):
98 self.sk.write("Status: %s\n" % status)
99 for nm, val in headers:
100 self.sk.write("%s: %s\n" % (nm, val))
103 raise ashd.serve.closed()
105 def writedata(self, data):
110 raise ashd.serve.closed()
113 head = ashd.scgi.readhead(self.sk)
114 self.env = mkenv(head, self.sk)
115 reqevent = ashd.perf.request(self.env)
116 exc = (None, None, None)
118 super(reqthread, self).handle()
120 reqevent.response([self.status, self.headers])
125 reqevent.__exit__(*exc)
129 super(reqthread, self).run()
135 nsk, addr = sk.accept()
137 reqthread(nsk).start()