Fwd: [Twisted-web] Setting up a project.
![](https://secure.gravatar.com/avatar/7220ab6704394a472b679f3ed30934eb.jpg?s=120&d=mm&r=g)
On Wed, Aug 6, 2008 at 4:27 AM, Phil Mayers <p.mayers@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
![](https://secure.gravatar.com/avatar/a61e243764490913906c773c9acb0d3c.jpg?s=120&d=mm&r=g)
"Govind Salinas" <blix@sophiasuchtig.com> writes:
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
Remember, with twisted.web, for the most part aside from pure HTTP processing, not much is going to happen for free without explicitly being coded. There are some utility method (like for request sessions) and some utility modules (like static.py for file serving), but your code is pretty much in charge, for better or worse :-)
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.
twisted.web's static.File implementation does the content type based on file extension, using Python's internal mimetypes module dictionary, some custom additions, and anything in /etc/mime.types by default. You can also augment class or instance contentTypes variable if needed to apply your own mappings, as well as establish a default mapping for a file/directory for non-matching files when you instantiate static.File (see the favicon usage in my last post). For resources that aren't static and identifiable by file extension, yes, you'll need to establish the correct content type as part of your resource handler during request processing if it's not going to be HTML.
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.
Depends on what you want the user to see in such a case. I don't think anything more than that is truly required for correct operation protocol wise. The resource is still in control of what will be rendered, so you can use your own template, static content, etc.. in addition to setting the response code. twisted.web.error has some simple resources that render some plain text and set the appropriate HTTP response code, but yes, anything much above that is your responsibility. The only exception to this is if displayTracebacks is set to True (the default) in your Site object. In that case, exceptions/Failures that reach the top level will be rendered nicely to the browser (using twisted.web.util.formatFailure).
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.
Sounds like you may be looking for a more extensive framework, perhaps with a wsgi interface. twisted.web is largely a protocol implementation. -- David
participants (2)
-
David Bolen
-
Govind Salinas