From: Fredrik Tolf Date: Sun, 5 Jan 2014 09:31:27 +0000 (+0100) Subject: python: Added command-line parsing of request handler and its arguments. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=8db41888f19bbb6b26bb084287716b6832d6ec78;p=ashd.git python: Added command-line parsing of request handler and its arguments. --- diff --git a/python3/ashd-wsgi3 b/python3/ashd-wsgi3 index b250bce..a07ac8d 100755 --- a/python3/ashd-wsgi3 +++ b/python3/ashd-wsgi3 @@ -8,12 +8,12 @@ except: pdm = None def usage(out): - out.write("usage: ashd-wsgi3 [-hAL] [-m PDM-SPEC] [-p MODPATH] [-l REQLIMIT] HANDLER-MODULE [ARGS...]\n") + out.write("usage: ashd-wsgi3 [-hAL] [-m PDM-SPEC] [-p MODPATH] [-t REQUEST-HANDLER[:PAR[=VAL](,PAR[=VAL])...]] HANDLER-MODULE [ARGS...]\n") -reqlimit = 0 +hspec = "free", {} modwsgi_compat = False setlog = True -opts, args = getopt.getopt(sys.argv[1:], "+hALp:l:m:") +opts, args = getopt.getopt(sys.argv[1:], "+hALp:t:m:") for o, a in opts: if o == "-h": usage(sys.stdout) @@ -24,8 +24,8 @@ for o, a in opts: setlog = False elif o == "-A": modwsgi_compat = True - elif o == "-l": - reqlimit = int(a) + elif o == "-t": + hspec = ashd.serve.parsehspec(a) elif o == "-m": if pdm is not None: pdm.srv.listen(a) @@ -145,19 +145,12 @@ def mkenv(req): env["wsgi.run_once"] = False return env -if reqlimit != 0: - guard = ashd.serve.abortlimiter(reqlimit).call -else: - guard = lambda fun: fun() - def recode(thing): if isinstance(thing, collections.ByteString): return thing else: return str(thing).encode("latin-1") -reqhandler = ashd.serve.freethread() - class request(ashd.serve.wsgirequest): def __init__(self, *, bkreq, **kw): super().__init__(**kw) @@ -192,6 +185,17 @@ class request(ashd.serve.wsgirequest): def handle(req): reqhandler.handle(request(bkreq=req, handler=reqhandler)) +if hspec[0] not in ashd.serve.names: + sys.stderr.write("ashd-wsgi3: no such request handler: %s\n" % hspec[0]) + sys.exit(1) +hclass = ashd.serve.names[hspec[0]] +try: + hargs = hclass.parseargs(**hspec[1]) +except ValueError as exc: + sys.stderr.write("ashd-wsgi3: %s\n" % exc) + sys.exit(1) + +reqhandler = hclass(**hargs) try: ashd.util.serveloop(handle) finally: diff --git a/python3/ashd/serve.py b/python3/ashd/serve.py index d78cec9..8520411 100644 --- a/python3/ashd/serve.py +++ b/python3/ashd/serve.py @@ -79,6 +79,12 @@ class handler(object): def close(self): pass + @classmethod + def parseargs(cls, **args): + if len(args) > 0: + raise ValueError("unknown handler argument: " + next(iter(args))) + return {} + class freethread(handler): def __init__(self, **kw): super().__init__(**kw) @@ -124,11 +130,11 @@ class freethread(handler): if len(self.current) > 0: th = next(iter(self.current)) else: - th = None + return th.join() class threadpool(handler): - def __init__(self, *, min=0, max=20, live=10, **kw): + def __init__(self, *, min=0, max=20, live=300, **kw): super().__init__(**kw) self.current = set() self.free = set() @@ -142,6 +148,17 @@ class threadpool(handler): for i in range(self.min): self.newthread() + @classmethod + def parseargs(cls, *, min=None, max=None, live=None, **args): + ret = super().parseargs(**args) + if min: + ret["min"] = int(min) + if max: + ret["max"] = int(max) + if live: + ret["live"] = int(live) + return ret + def newthread(self): with self.lk: th = reqthread(target=self.loop) @@ -228,3 +245,20 @@ class threadpool(handler): names = {"free": freethread, "pool": threadpool} + +def parsehspec(spec): + if ":" not in spec: + return spec, {} + nm, spec = spec.split(":", 1) + args = {} + while spec: + if "," in spec: + part, spec = spec.split(",", 1) + else: + part, spec = spec, None + if "=" in part: + key, val = part.split("=", 1) + else: + key, val = part, "" + args[key] = val + return nm, args diff --git a/python3/scgi-wsgi3 b/python3/scgi-wsgi3 index 428767f..b2ec1ca 100755 --- a/python3/scgi-wsgi3 +++ b/python3/scgi-wsgi3 @@ -9,12 +9,13 @@ except: pdm = None def usage(out): - out.write("usage: scgi-wsgi3 [-hAL] [-m PDM-SPEC] [-p MODPATH] [-T [HOST:]PORT] HANDLER-MODULE [ARGS...]\n") + out.write("usage: scgi-wsgi3 [-hAL] [-m PDM-SPEC] [-p MODPATH] [-t REQUEST-HANDLER[:PAR[=VAL](,PAR[=VAL])...]] [-T [HOST:]PORT] HANDLER-MODULE [ARGS...]\n") sk = None +hspec = "free", {} modwsgi_compat = False setlog = True -opts, args = getopt.getopt(sys.argv[1:], "+hALp:T:m:") +opts, args = getopt.getopt(sys.argv[1:], "+hALp:t:T:m:") for o, a in opts: if o == "-h": usage(sys.stdout) @@ -40,6 +41,8 @@ for o, a in opts: elif o == "-m": if pdm is not None: pdm.srv.listen(a) + elif o == "-t": + hspec = ashd.serve.parsehspec(a) if len(args) < 1: usage(sys.stderr) sys.exit(1) @@ -95,8 +98,6 @@ def recode(thing): else: return str(thing).encode("latin-1") -reqhandler = ashd.serve.freethread() - class request(ashd.serve.wsgirequest): def __init__(self, *, sk, **kw): super().__init__(**kw) @@ -130,6 +131,17 @@ class request(ashd.serve.wsgirequest): self.sk.close() self.bsk.close() +if hspec[0] not in ashd.serve.names: + sys.stderr.write("ashd-wsgi3: no such request handler: %s\n" % hspec[0]) + sys.exit(1) +hclass = ashd.serve.names[hspec[0]] +try: + hargs = hclass.parseargs(**hspec[1]) +except ValueError as exc: + sys.stderr.write("ashd-wsgi3: %s\n" % exc) + sys.exit(1) + +reqhandler = hclass(**hargs) try: while True: nsk, addr = sk.accept()