23

I have been using PHP for years. Lately I've come across numerous forum posts stating that PHP is outdated, that modern programming languages are easier, more secure, etc. etc.

So, I decided to start learning Python. Since I'm used to using PHP, I just started building pages by uploading an .htaccess file with:

addtype text/html py
addhandler cgi-script .py

Then, my sample pages look like:

#!/usr/bin/python
print "content-type: text/html\n\n"
print "html tags, more stuff, etc."

This works fine. But, I came across a comment in a post that said that CGI isn't the best way to use Python. Of course, it didn't mention what is the best way.

Why is it that using CGI is not the best way to use Python? What is the alternative?

Is there some totally other way to set up a simple Python site? Is there some completely different paradigm I should be looking at outside of .htaccess and .py files?

Related

6 Answers 6

13

Classic CGI isn't the best way to use anything at all. With classic CGI server has to spawn a new process for every request.

As for Python, you have few alternatives:

3
  • I wouldn't recommend simplehttpserver for an actual production webapp. It's more for having an embedded webserver in other applications. As a replacement, might I recommend cherrypy - cherrypy.org ? Mar 10, 2009 at 12:44
  • One could also note that you can use a different, smaller proxying webserver like nginx or lighttpd in place of apache.
    – epochwolf
    Mar 10, 2009 at 13:39
  • 2
    For shared hosting service users -- and that probably means most readers of this post -- this constraint isn't really relevant because such shared hosting configurations invariably use UID/GID access control to separate individual user accounts and therefor always spawn a separate process for any scripted URI -- PHP, Python of whatever.
    – TerryE
    Jan 24, 2012 at 16:02
5

Why is it that using CGI is not the best way to use Python?

I will stick up for CGI a little. It's good for development environments.

It's simple to wire up and you don't have to worry about module reloading problems. Naturally performance is terrible, but for dev you don't care.

Of course you should really be writing to the WSGI interface rather than CGI directly. You can then deploy through CGI using:

wsgiref.handlers.CGIHandler().run(application)

and use the same application object to deploy through mod_wsgi other whatever other WSGI server you prefer in the production environment where speed matters (and the testing environment where you want it to be as close to production as possible).

3

mod_wsgi is the proper alternative. It is preferable over CGI in almost all aspects.

3
  • Great if you control your own LAMP stack on a VM or dedicated box, but can you identify a shared hosting provider that actually loads and enables mod_wsgi?
    – TerryE
    Jan 24, 2012 at 16:04
  • Nope. But no one explicitly asked about shared hosting.
    – Yuval Adam
    Jan 24, 2012 at 17:37
  • I agree Yuval, but given that the significant majority of hosting users use a shared hosting account, I assume including the original poster (since he's doing this via .htaccess files and not his vhost config). Isn't it just as flawed to assume that the original poster has root access to his web service. Perhaps a caveat "CGI in almost all aspects, except of course in the most common case of a shared hosting account where you won't be able to use it" ;-P
    – TerryE
    Jan 24, 2012 at 17:51
3

Really it's just an efficiency thing - CGI spawns an entire new process for every request, which is quite heavyweight for what it does.

PHP can be run through CGI as well, but mod_php embeds the interpreter within apache. There's a mod_python which does the same job, and mod_wsgi as Yuval says.

2

There is a page in the Python documentation that describes the advantages and disadvantages of the various possibilities.

mod_python

(…) These are the reasons why mod_python should be avoided when writing new programs.

WSGI

The Web Server Gateway Interface or WSGI for short is currently the best possible way to Python web programming. While it is great for programmers writing frameworks, the normal person does not need to get in direct contact with it.

FastCGI and stuff

These days, FastCGI is never used directly.

3
  • FastCGI is heavily used by shared hosting environments. I personally use PHP with FastCGI (using nginx instead of apache) for some stuff at home.
    – epochwolf
    Mar 10, 2009 at 13:40
  • Note that the post says that FastCGI is never used directly (presumably in the context of Python). This is pretty much consistent with my experience. Mar 10, 2009 at 14:01
  • Actually that's what the documentation says. Mar 10, 2009 at 14:04
2

Aside from the suggestions others make, you should really consider using a framework of some kind. You can and should be using FastCGI, mod_python, or mod_wsgi, but they weren't really intended for you to write code directly against. Might I suggest one of the following?

2
  • +1: "some completely different paradigm I should be looking at outside of .htaccess and .py files"
    – S.Lott
    Mar 10, 2009 at 12:46
  • I am learning Pylons at the same time, but it's nice to use a framework where I can also program in the underlying language.
    – Andy Swift
    Mar 10, 2009 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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