examples: setsid is now built into htparser.
[ashd.git] / python / scgi-wsgi
... / ...
CommitLineData
1#!/usr/bin/python
2
3import sys, os, getopt, logging
4import socket
5import ashd.scgi
6
7def usage(out):
8 out.write("usage: scgi-wsgi [-hAL] [-p MODPATH] [-T [HOST:]PORT] HANDLER-MODULE [ARGS...]\n")
9
10sk = None
11modwsgi_compat = False
12setlog = True
13opts, args = getopt.getopt(sys.argv[1:], "+hALp:T:")
14for o, a in opts:
15 if o == "-h":
16 usage(sys.stdout)
17 sys.exit(0)
18 elif o == "-p":
19 sys.path.insert(0, a)
20 elif o == "-L":
21 setlog = False
22 elif o == "-T":
23 sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
24 sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
25 p = a.rfind(":")
26 if p < 0:
27 bindhost = "localhost"
28 bindport = int(a)
29 else:
30 bindhost = a[:p]
31 bindport = int(a[p + 1:])
32 sk.bind((bindhost, bindport))
33 sk.listen(32)
34 elif o == "-A":
35 modwsgi_compat = True
36if len(args) < 1:
37 usage(sys.stderr)
38 sys.exit(1)
39if setlog:
40 logging.basicConfig(format="scgi-wsgi(%(name)s): %(levelname)s: %(message)s")
41
42if sk is None:
43 # This is suboptimal, since the socket on stdin is not necessarily
44 # AF_UNIX, but Python does not seem to offer any way around it,
45 # that I can find.
46 sk = socket.fromfd(0, socket.AF_UNIX, socket.SOCK_STREAM)
47
48try:
49 handlermod = __import__(args[0], fromlist = ["dummy"])
50except ImportError, exc:
51 sys.stderr.write("scgi-wsgi: handler %s not found: %s\n" % (args[0], exc.message))
52 sys.exit(1)
53if not modwsgi_compat:
54 if not hasattr(handlermod, "wmain"):
55 sys.stderr.write("scgi-wsgi: handler %s has no `wmain' function\n" % args[0])
56 sys.exit(1)
57 handler = handlermod.wmain(*args[1:])
58else:
59 if not hasattr(handlermod, "application"):
60 sys.stderr.write("scgi-wsgi: handler %s has no `application' object\n" % args[0])
61 sys.exit(1)
62 handler = handlermod.application
63
64ashd.scgi.servescgi(sk, ashd.scgi.wrapwsgi(handler))