[Twisted-web] Turkey Questions 1-5

Matt Goodall matt at pollenation.net
Thu Jul 7 18:32:29 MDT 2005


lloyd at paisite.com wrote:

>Hello,
>
>I've changed the thread from "Re: Introduction, newbie confusion plus an
>offer" to "Turkey Questions 1-5" to start the Q&A.
>
>Goal: Create the world's simplest web-server
>
>My first set of questions are based on "Configuring and Using the
>Twisted.Web Server in "Twisted.Web Documentation."
>
>Inspired by the section "Installing a pre-configured server," I did the
>following:
>
>-- created the directory ~/twisted/www
>-- created the file ~/twisted/www/myHTML.html
>
><html>
><head>
><title>flat html file</title>
></head>
><body>
><h1>Hello, World!</h1>
></body>
></html>
>
>-- entered "mktap web --path ~/twisted/www --logfile ~/twisted/www --port
>8080" into the command line
>-- entered "twistd -f web.tap" into command line
>
>Results:
>
>-- System wrote the file "web.tap" into ~$, my home directory
>-- System wrote twistd.log into ~/twisted/www
>-- When I entered "http://localhost:8080/myHTML.html, my browser displayed
>"Hello, World!"
>
>OK, success.
>
>Questions:
>
>1) Why did mktap write web.tap to my home directory rather than
>~/twisted/www?
>  
>
Presumably because you ran mktap from your home directory. The --path is
the path to the root of the web server. The .tak file is written to the
current directory.

>2) Why didn't myHTML.html require a "Content-type: text/html" line?
>  
>
The web.tap you created actually uses a twisted.web.static.File instance
to serve the files from the --path directory. static.File guesses the
MIME type and sends the Content-Type response header to the browser.

>3) How can I see the source for the server created by mktap?
>  
>
Good question and, to be honest, I rarely use mktap. I find it useful
for quickly creating a web server for static content but most of the
stuff I use is highly dynamic.

Here's an equivalent .tac file that serves static content:

    from twisted.application import internet, service
    from twisted.web import server, static

    WWWROOT = '/home/matt/www/docs'
    LOGPATH = '/home/matt/www/logs/access.log'
    PORT = 8080

    application = service.Application('web-server')
    site = server.Site(static.File(WWWROOT), logPath=LOGPATH)
    internet.TCPServer(PORT, site).setServiceParent(application)


That sort of thing can be easily modified into something that creates a
dynamic root resource.

>4) Are there preferred practices for creating directories for the document
>root (for instance, better in user or root), directories for resource
>modules, site data, css styling, etc.?
>  
>
I never use .rpy resources so all my Python is in packages and modules.
I tend to have templates, css, js and images in a directory somewhere
and a .tac to start the services.

Site data is either in a database or in another directory, away from the
templates etc.

>5) Are there preferred practices for assigning permissions to files in the
>above directories?
>  
>
Well, I think the most important thing is to avoid making *any* code or
data directly accessible from the web. That's fairly easy to do if you
don't use .rpy files and make sure that any directories served by
static.File resources don't contain any files with sensitive content.
XHTML templates should not be directly accessible over the web either.

Hope some of that is useful.

- 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