OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 # | |
5 # Author: Clark Evans (cce@clarkevans.com) | |
6 | |
7 """ flow.web | |
8 | |
9 This contains wrappers to apply flow to components in twisted.web.* | |
10 | |
11 """ | |
12 from controller import Deferred | |
13 from twisted.web import resource, server | |
14 from twisted.python.failure import Failure | |
15 | |
16 class Resource(resource.Resource): | |
17 """ | |
18 A resource which uses flow in its page generation. | |
19 | |
20 Use it like this:: | |
21 | |
22 from __future__ import generators | |
23 from twisted.flow import flow | |
24 | |
25 def render(req): | |
26 req.write("<html><head><title>Delayed</title></head>") | |
27 req.write("<body><h1>Delayed WebPage</h1>") | |
28 yield flow.Cooperate(5) | |
29 req.write("<p>Delayed Content</p></body></html>") | |
30 | |
31 if __name__=='__main__': | |
32 from twisted.internet import reactor | |
33 from twisted.web.server import Site | |
34 from twisted.flow.web import Resource | |
35 print "visit http://localhost:8081/ to view" | |
36 reactor.listenTCP(8081,Site(Resource(render))) | |
37 reactor.run() | |
38 """ | |
39 def __init__(self, gen): | |
40 resource.Resource.__init__(self) | |
41 self.gen = gen | |
42 | |
43 def isLeaf(self): | |
44 return true | |
45 | |
46 def render(self, req): | |
47 self.d = Deferred(self.gen(req)) | |
48 self.d.addErrback(lambda fail: fail.printTraceback()) | |
49 self.d.addBoth(lambda ret: req.finish() or ret) | |
50 return server.NOT_DONE_YET | |
OLD | NEW |