Commit | Line | Data |
---|---|---|
173e0e9e | 1 | #!/usr/bin/python |
c06db49a | 2 | |
78c8462c | 3 | import sys, os, getopt, logging |
c06db49a FT |
4 | import socket |
5 | import ashd.scgi | |
6 | ||
7 | def usage(out): | |
78c8462c | 8 | out.write("usage: scgi-wsgi [-hAL] [-p MODPATH] [-T [HOST:]PORT] HANDLER-MODULE [ARGS...]\n") |
c06db49a FT |
9 | |
10 | sk = None | |
11 | modwsgi_compat = False | |
78c8462c FT |
12 | setlog = True |
13 | opts, args = getopt.getopt(sys.argv[1:], "+hALp:T:") | |
c06db49a FT |
14 | for o, a in opts: |
15 | if o == "-h": | |
16 | usage(sys.stdout) | |
17 | sys.exit(0) | |
18 | elif o == "-p": | |
e4769c65 | 19 | sys.path.insert(0, a) |
78c8462c FT |
20 | elif o == "-L": |
21 | setlog = False | |
c06db49a FT |
22 | elif o == "-T": |
23 | sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
78c8462c | 24 | sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
c06db49a FT |
25 | p = a.rfind(":") |
26 | if p < 0: | |
075a379e | 27 | bindhost = "localhost" |
c06db49a FT |
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 | |
36 | if len(args) < 1: | |
37 | usage(sys.stderr) | |
38 | sys.exit(1) | |
78c8462c | 39 | if setlog: |
b327e4c1 | 40 | logging.basicConfig(format="scgi-wsgi(%(name)s): %(levelname)s: %(message)s") |
c06db49a FT |
41 | |
42 | if 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 | ||
48 | try: | |
49 | handlermod = __import__(args[0], fromlist = ["dummy"]) | |
173e0e9e FT |
50 | except ImportError, exc: |
51 | sys.stderr.write("scgi-wsgi: handler %s not found: %s\n" % (args[0], exc.message)) | |
c06db49a FT |
52 | sys.exit(1) |
53 | if 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) | |
adb11d5f | 57 | handler = handlermod.wmain(*args[1:]) |
c06db49a FT |
58 | else: |
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 | ||
64 | ashd.scgi.servescgi(sk, ashd.scgi.wrapwsgi(handler)) |