Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I only know server side programming with PHP. To me, I write something into index.php and call http://localhost/index.php and see the results of the PHP script. I have been using CakePHP and understand that there is something you can do with URL rewriting, but in my mind, there is this one to one relation between URLs and PHP files on the server.

Now I do a lot with Python, but just command line utilites. I'd like to use Python instead of PHP for my next project. So far, I was told about mod_python, that I have to use a framework like Pylons or Twisted or even Django to get any output at all. Others have said that I just have to print() a HTTP header and then the script would output like I am used with PHP.

I would like to write something that basically only takes a directory listing and makes it pretty, so that I can access a bunch of PDF files from a mobile browser. I know how to do this with PHP, I know how to create a Python script that would generate the HTML. I just do not know how to execute that script when I open http://localhost/pdfcentral/.

How would I have to configure my server? It runs on Ubuntu 10.04.

share|improve this question
    
There are many variations you can take but the simplest way would be to put the Python script in your cgi-bin directory so the server knows to execute it, and optionally make a PHP file redirect to it if you wish to use a different path. Regarding the Python script itself you can follow this procedure: forrestbao.blogspot.com/2009/08/… –  ionFish Jan 29 '13 at 22:16
add comment

2 Answers 2

up vote 5 down vote accepted

mod_python is not being actively developed anymore. While Apache says that doesn't mean the project is dead -

Currently mod_python is not under active development. This does not mean that it is "dead" as some people have claimed. It smiply means that the code and the project are mature enough when very little is required to maintain it.

But, it is suggested that one should use mod_wsgi instead. That is if you want to build everything from scratch. Which I suggest, you shouldn't, unless you are using this as a learning exercise.

It would be better to use a web-framework to do the heavy lifting - handling routing, requests, and the likes

A comprehensive list of Python web-frameworks is available here, but here are some that are popular -

  • Django - It is considered the Python equivalent of Ruby on Rails, and is pretty heavy duty, so wouldn't be a good fit for what you need.
  • Flask - It is a microframework. That is, it is small, doesn't get in your way, and is ideal for smaller projects that don't require a lot of batteries included.
  • Pyramid - It is built by the same group of people who built Pylons, which is no defunct. It is somewhere in between Flask and Django - can be used as a replacement for either.
  • Bottle - This is another microframework, and is even more minimal than Flask. Is just one file and has no dependencies(if you use the built in templating engine) other than the standard library, so would be a nice fit for what you want.
  • Web.py - Once powered Reddit(which was rewritten with new tools after being acquired).
  • webapp2 - It is the standard framework for the Google App Engine(though you can use other frameworks there).

Of these, I would recommend Flask, as I've used it and have no complaints. It also has a lot of extensions that would make life a lot easier if you want more features in your site.

Also, you would need a template engine - to assemble your html. Jinja2, built by the same guys who make Flask, and is pretty widely used. But coming from php web development, you might find using Mako more natural, which is pretty popular too.

Here is a more comprehensive faq on Python Web development - Python FAQ: Webdev.

share|improve this answer
    
I got a little Flask project now running with FastCGI und lighttpd on a test machine, it should run on the server as well. Thank you very much! –  queueoverflow Feb 1 '13 at 18:40
add comment

I found the question a little confusing and If I'm right I assume that your main objective is to serve files to your mobile phone using pretty styling?

If that is true then you shouldn't need to go though so many loops to set something up.

Apache2 allows you to edit the main file listing directory so you can style it pretty much anyway you want.

There is also many available ready main themes online just search Google for Theme for Apache2 Directory Listing, making it responsive wouldn't be to hard either.

Here's some information how to change the default look of Apache's

And heres some ready made templates to check out, there's many more....

Repos Style

Recurser

Indices

share|improve this answer
    
This seems to solve the initial problem pretty well. I thought that I could use this as a first project to move from PHP to Python. If the Python project stalls, I will take a look at this. Thanks! –  queueoverflow Jan 30 '13 at 8:03
    
Your welcome ;) –  bybe Jan 30 '13 at 16:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.