Skip to content

Commit

Permalink
now with moar consumers per server
Browse files Browse the repository at this point in the history
  • Loading branch information
rndmcnlly committed Jan 26, 2009
1 parent 6e3093a commit a7e03f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 12 additions & 0 deletions consumers/README
Expand Up @@ -30,3 +30,15 @@ consider this variant that the module also supports:
> def f(request):
> print request
> return "whatevz!"

The "serve" function is quite generous in what it accepts for its first
argument. Assuming you have defined 'f' and 'g' as consumers as before you can
create a server from both consumers at once using syntax like this:

> serve({'letter_f':f,'letter_g':g})

or simply

> serve([f,g])

if you' like '/f' to be handled by f and '/g' to be handled by g.
13 changes: 12 additions & 1 deletion consumers/consumer.py
Expand Up @@ -5,11 +5,22 @@ def consumer(f):
class R(resource.Resource):
isLeaf = True
R.render = lambda res, req: f(req)
R.tag = f.__name__
return R()

def serve(res, port):
if type(res) is dict:
root = resource.Resource()
for k,v in res.iteritems():
root.putChild(k,v)
res = root
if type(res) is list:
root = resource.Resource()
for r in res:
root.putChild(r.tag, r)
res = root
reactor.listenTCP(port, server.Site(res))
reactor.run()

def easy_consume(port):
def easy_consume(port=80):
return lambda f: serve(consumer(f),port)

0 comments on commit a7e03f6

Please sign in to comment.