4
from slides import Lecture, Slide, TitleSlide, Image, Bullet, PRE, URL, SubBullet, NumSlide, toHTML
7
from twisted.web import static, twcgi
9
class PerlScript(twcgi.FilteredScript):
10
filter = '/usr/bin/perl' # Points to the perl parser
14
from twisted.web import resource
16
class MyGreatResource(resource.Resource):
17
def render(self, request):
18
return "<html>foo</html>"
20
resource = MyGreatResource()
24
"A Twisted Web Tutorial",
26
TitleSlide("Twisted Web -- A tutorial",
27
Image("twistedlogo.png"),
30
Slide("Twisted Web -- Where does it fit?",
31
Image("twisted-overview.png"),
34
Slide("Setup and Configuration Utilities",
42
Bullet("General usage"),
43
Bullet("Flexibility and Power"),
46
Slide("mktap web : Common Useful Options",
51
Bullet("--processor"),
54
Slide("Sample mktap command lines",
55
Bullet(PRE("mktap web")),
56
Bullet(PRE("mktap web --path=/var/www --logfile=/var/log/twistedweb.log")),
57
Bullet(PRE("mktap web --port=80 --path=/var/www --user --mime-type=text/plain")),
58
Bullet(PRE("mktap web --path=/home/nafai/public_html --processor=.pl=PerlProcessor.PerlProcessor --index=index.pl")),
61
Slide("twistd : An overview",
62
Bullet("Start a Twisted Application"),
63
Bullet("Loads and instance of twisted.internet.app.Application from a file"),
64
Bullet("Daemonizes, binds to appropriate ports, and starts the Twisted mainloop"),
67
Slide("Sample twistd command lines",
68
Bullet(PRE("twistd -f web.tap -l /var/log/twisted.log")),
69
Bullet(PRE("twistd -f web.tap --pidfile /var/run/web.pid")),
72
Slide("Shutting down twistd",
73
Bullet("On Unix (in general): "),
75
Bullet(PRE("kill -9 `cat twistd.pid`"))),
76
Bullet("On Windows: "),
78
Bullet("Cannot daemonize on Windows, so just run twistd in a command prompt"),
79
Bullet("Switch to the command prompt, and press Control-C"),
83
Slide("Shutdown TAPs",
84
Bullet("Since TAPs store persistent data for an application,\
85
a 'shutdown' TAP is created on twistd shutdown"),
86
Bullet("You'll often want to start your Twisted application\
87
on subsequent runs with the shutdown TAP"),
90
Slide("twistd and security",
91
Bullet("When twistd is run as root, it will shed root privileges\
92
for the uid and gid of either the user that created the TAP or those\
93
specified on the mktap commandline."),
98
Bullet("Used to change what the root of the server points to"),
99
Bullet("Set it to a Resource contained either in a Python source file or a Pickle file"),
102
Slide("Sample websetroot command lines",
103
Bullet(PRE("websetroot -p 80 -f web.tap --script rootResource.py")),
104
Bullet(PRE("websetroot -p 8080 -f web.tap --pickle rootPickle")),
108
# Use the perl example from the docs
109
Slide("What's a Resource?",
110
Bullet("Everything in twisted in represented as twisted.web.resource.Resource object"),
111
Bullet("In general, two calls are made on a resource:"),
113
Bullet(PRE("getChild()")),
114
Bullet(PRE("render()")),
118
Slide("Resource call examples:",
119
Bullet("/foo/bar/baz gets converted to:"
122
Bullet(PRE("site.getChild('foo', request).getChild('bar', request).getChild('baz', request).render(request)")),
124
Bullet("/foo/bar/baz/ gets converted to:"
127
Bullet(PRE("site.getChild('foo', request).getChild('bar', request).getChild('baz', request).getChild('', request).render(request)")),
131
Slide("What do Resources handle?",
132
Bullet("Out of the box, Twisted supports files of all types"),
133
Bullet("HTML, text, etc."),
134
Bullet("Default MIME type can be specified"),
137
Slide("What about web development?",
138
Bullet("Twisted Web has what are called processors, which are instances\
139
of classes inherited from resource.Resource"),
140
Bullet("By default, Twisted supports the following file types:"),
149
Bullet("You can also write your own"),
152
Slide("Custom Processor: More than One Evil Way to Do It",
153
Bullet("A custom processor to handle Perl CGIs:"),
155
Bullet("An example of how to use:"),
156
SubBullet(Bullet(PRE("mktap web --path=/home/nafai/public_html --processor=.pl=PerlScript.PerlScript")),
160
Slide("What about making my own resources?",
161
Bullet("Define a class that inherits from resource.Resource"),
162
Bullet("Define the render() method on that class"),
163
Bullet("For long requests, render() can return NOT_DONE_YET"),
164
Bullet("Then Create a .rpy file that sets resource = to an instance of the class"),
167
Slide(".rpy example",
172
Bullet("In other words, the slides I didn't get to write..."),
174
Bullet("Distributed Servers"),
175
Bullet("Virtual Hosts"),
176
Bullet("Rewrite Rules"),
177
Bullet("Debian configuration"),
178
Bullet("twistedmatrix.com configuration"),
183
if __name__ == '__main__':
184
lecture.renderHTML(".", "tw_deploy-%02d.html", css="main.css")