Skip to content

Commit a7e03f6

Browse files
committed
now with moar consumers per server
1 parent 6e3093a commit a7e03f6

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

consumers/README

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,15 @@ consider this variant that the module also supports:
3030
> def f(request):
3131
> print request
3232
> return "whatevz!"
33+
34+
The "serve" function is quite generous in what it accepts for its first
35+
argument. Assuming you have defined 'f' and 'g' as consumers as before you can
36+
create a server from both consumers at once using syntax like this:
37+
38+
> serve({'letter_f':f,'letter_g':g})
39+
40+
or simply
41+
42+
> serve([f,g])
43+
44+
if you' like '/f' to be handled by f and '/g' to be handled by g.

consumers/consumer.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@ def consumer(f):
55
class R(resource.Resource):
66
isLeaf = True
77
R.render = lambda res, req: f(req)
8+
R.tag = f.__name__
89
return R()
910

1011
def serve(res, port):
12+
if type(res) is dict:
13+
root = resource.Resource()
14+
for k,v in res.iteritems():
15+
root.putChild(k,v)
16+
res = root
17+
if type(res) is list:
18+
root = resource.Resource()
19+
for r in res:
20+
root.putChild(r.tag, r)
21+
res = root
1122
reactor.listenTCP(port, server.Site(res))
1223
reactor.run()
1324

14-
def easy_consume(port):
25+
def easy_consume(port=80):
1526
return lambda f: serve(consumer(f),port)

0 commit comments

Comments
 (0)