[Twisted-web] Turkey questions 13-15
Matt Goodall
matt at pollenation.net
Mon Jul 25 16:52:58 MDT 2005
lloyd at paisite.com wrote:
> Hello,
>
> Thanks to Matt Goodall and Nils Grotnes, Techno Turkey's Twisted Adventure
> is coming along at a slow, but sure pace. See http://twisted.paisite.com/
>
> If you scroll down to Experiments.Twisted.Web you'll see the latest
> experiment, Matt Goodall's Barebones Static Web Server.
>
> I've done the best I can to explicate Matt's code, but not sure that I've
> got it exactly right. Here're some questions:
>
> Turkey question 13:
>
> Any errors, misunderstandings, or confusing explanations so far in
> Adventures?
Looks pretty good.
>
> Turkey question 14:
>
> In the document "Using Application" the term "service" is used in a
> technical sense. Could someone please give me a clear definition in plain
> English?
Hmm, tricky. "Service" is a fairly meaningless term because it's so general.
I guess all I can say is that a service is some important part of your
application that is started and stopped in a controlled manner, i.e as
the application itself is started and stopped.
Typically a service manages resources that, without the predictable
startup and shutdown sequence, could be started too soon or left dangling.
Examples are:
* a storage service that must open and close the store to avoid corruption
* an integration service that opens a connection to some remote server
* a mail service that creates and manages smtp, pop and imap servers
>
> Turkey question 15:
>
> In an e-mail, Matt pointed out that the Barebones code could be "easily
> modified into something that creates a dynamic root resource."
>
> What's the next logical step toward this goal? E.g. How can I modify and/or
> add to the barebones code to support dynamic resources if, say, my
> ultimate goal is an efficient, solid, secure production web-server that
> supports virtual hosts and dynamic resources?
Here's a modified version of the barebones server that renders dynamic
pages instead of static pages:
-----------------------
from twisted.application import internet, service
from twisted.web import resource, server
LOGPATH = './logs/access.log'
PORT = 8080
class DynamicResource(resource.Resource):
"""A simple, dynamic resource with a name.
"""
def __init__(self, name):
resource.Resource.__init__(self)
self.name = name
def getChild(self, name, request):
# If the child's name is '' - the empty segment - then return
# myself.
if name == '':
return self
# Return an instance of my type with the requested segment name.
return DynamicResource(name)
def render(self, request):
return '<html><p>I am a DynamicResource and my name is
%r.</p></html>' % (self.name,)
application = service.Application('web-server')
site = server.Site(DynamicResource('root'), logPath=LOGPATH)
internet.TCPServer(PORT, site).setServiceParent(application)
-----------------------
There's really no conceptual difference between a DynamicResource and a
static.File resource. The only difference is how they choose to render
the request.
A couple of things worth pointing out here:
* templating like that sucks. don't do it. use nevow, or one of the
many templating libraries available for Python.
* the name == '' bit in getChild is important for the root resource of
a site but you often don't want that for resources deeper into the
URL.
One other thing that needs mentioning is that the above is a twisted.web
resource. twisted.web2 (currently in development) is more flexible and
has a much stronger concept of the Response. To avoid confusion, I won't
say anymore about this. Go and read the code if you're interested.
To learn about virtual hosts you should read the "Virtual Hosts" section
of the twisted.web HOWTO.
http://twistedmatrix.com/projects/web/documentation/howto/using-twistedweb.html
>
> In the spirit of Moshe's Finger tutorial, I'd like to take this
> step-by-baby-step, illustrating and explaining key Twisted
> interfaces/principles as I go.
>
> Many thanks,
No problem. Hope it helps.
Cheers, Matt
--
__
/ \__ Matt Goodall, Pollenation Internet Ltd
\__/ \ w: http://www.pollenation.net
__/ \__/ e: matt at pollenation.net
/ \__/ \ t: +44 (0)113 2252500
\__/ \__/
/ \ Any views expressed are my own and do not necessarily
\__/ reflect the views of my employer.
More information about the Twisted-web
mailing list