Fwd: [Twisted-web] Setting up a project.
Govind Salinas
blix at sophiasuchtig.com
Wed Aug 6 12:13:16 EDT 2008
On Wed, Aug 6, 2008 at 4:27 AM, Phil Mayers <p.mayers at imperial.ac.uk> wrote:
> On Tue, Aug 05, 2008 at 06:30:23PM -0500, Govind Salinas wrote:
>>
>> Hi Folks,
>>
>> I am trying to set up an application that uses twisted.web for doing
>> some ad-hoc serving of data. If you are familiar with mercurial, what
>> i am doing is very similar to the "hg serve" command except it is for
>> use with git.
>>
>> So my question is, how should I compose the project. The how-tos and
>> examples left me not sure of what I should do. I have something that
>> runs and can serve up mako templates, but that is all it can do since
>> I am doing everything manually. I would prefer not to have to code in
>> serving things like css pages, 404 pages etc. I am sure this is
>> already in twisted.web somewhere.
>
>
> You haven't really given enough information.
>
> I presume you're using twisted.web "Resource" classes and http server?
>
Yes that is correct.
>> Let me give you some information about the set up I have . Basically
>> I have a python program running somewhere. I will have a default set
>> of files that serve the content a particular way. The default set
>> will contain mako templates[1], style sheets and perhaps some images.
>> So I need a way to kick off the mako templates based on the URL. The
>> templates may include each other.
>>
>> Here is the layout
>>
>> content-dir/
>> __config__.py -- This is a file that currently just sets up the mapping
>> between a regex that selects a page and a path
>> to
>> where that page's template is under the
>> content-dir
>> main.html -- The html files here are really the mako templates
>> main.css
>> etc...
>>
>> My server class takes in a template dir, loads the config and starts a
>> server. When it gets a request it matches it against the regexes and
>> runs the template. There should be some initialization that sets up
>> objects that get passed to the template (in addition to values from
>> the regex parsing). This is all pretty brain-dead code.
>
> Why does it need to be anything more than that? What's wrong with the
> current code?
There are a few things wrong with my current approach. One is that it
feels sort of like a hack the way I am doing it. It doesn't seem like the
right way. Second, since manually doing things that I normally expect
the web server to be doing, I expect there are 100 things that I should
be doing that I am not to act like a proper server. For instance, my
css pages wouldn't load so I am manually setting the content-type,
but *only* for css pages. If I end up needing other types of files, I
will have to add that in as well. Another example is that I am having
to roll my own page-not-found implementation. Aside from setting
the response code to 404, I am not sure what else I need to be doing.
Now, I have been able to get things to work for the simple pages that
I have put together, and it works rather well. But it seems limited and
fragile.
Is that more clear.
>> I was hoping for some guidance on a better strategy for doing this.
>
> Better how?
>
>> Any help would be appreciated. Bonus points if it is something that
>> could easily be re-used on another webserver if someone wanted to host
>> this on an existing website.
>
> I don't understand that bit.
This is not too important, I was hoping that there was a "framework" that I
could use that would be portable to other webservers if someone wanted
to do so.
>>
>> I tried to post this before signing up to the list and I was rejected. I
>> don't
>> know if you guys consider that a problem.
>>
>> [1] I am only using mako because it seems to suit my needs from a
>> templating perspective. If there is something built into twisted
>> that will do the job as well, then that would be one less dependency
>> that I have.
>
> You probably want to look at the Nevow toolkit which contains:
>
> * A slightly better application server / resource object hierarhcy on top of
> twisted.web - my understanding is this will eventually be backported into
> twisted.web
>
> * An XHTML & native-python deferred-aware templating engine
>
> * An AJAX/COMET server called Athena
>
> A little more detail about what's wrong with the current system would be
> helpful.
I think the problem is that, even having read the howtos, I do not feel like
I am making full use of the libraries in twisted.
I have not yet looked into newvow. However I have seen mention of .rpy
files in the documentation, but not a lot on the files themselves. Is there
a tutorial or some documentation on how to use rpy files? By that I mean
what should go inside them, setting up the server is explained in the
howtos.
-Govind
More information about the Twisted-web
mailing list