Wednesday, September 16, 2009

Twisted Web in 60 seconds: serve static content from a directory

Welcome to the first installment of "Twisted Web in 60 seconds". The goal of this installment is to show you how to serve static content from a filesystem using some APIs from Twisted Web (while Twisted also includes some command line tools, I will not be discussing those here) and have you understand it in 60 seconds or less (if you don't already know Python, you might want to stop here). Where possibly useful, I'll include links to the Twisted documentation, but consider these as tips for further exploration, not necessary prerequisites for understanding the example. So, let's dive in.

Bonus example! For those times when you don't actually want to write a new program, the above implemented functionality is one of the things which the command line twistd tool can do. In this case, the command twistd -n web --path /tmp will accomplish the same thing as the above server.

Keep an eye out of the next installment, in which I'll describe simple dynamic resources.

It doesn't seem to be be too complicated that is if I had to follow the exact steps you did here. I doubt though that it would work for any web content management system, I am worried that for my cms I won't ever find a way to add static content from a directory.

What do you think makes a "web content management system" special? If you constructed one on Twisted Web, and you wanted to be able to serve up static content from a directory, what do you think would prevent you from using static.File as I have here?

About Me

I'm a software professional with over 15 years industry experience ranging from startups (with as few as four people) to multinational banks. I've built network software, database software, user-facing software, backends, distributed systems, games, business engines, application servers, and more. I've lead teams and followed leaders (great and otherwise).

I'm also deeply interested and involved in environmental protection, clean food, and how agricultural systems impact human health. I live and work on a small farm with my family building first-hand experience with as many of the related systems as I can. You can read more about that side of my life on my other blog.

Supporting Open Source

I'm a prolific contributor to free and open source software projects, both on a volunteer and paid basis. I greatly appreciate donations to support the volunteer efforts. Feel free to let me know which software you're interested: it's always great to hear from users and a downside of a lot of this work is not getting to hear from the people who use the result very much.