[mod_python] XMLHttpRequest + CGI + mod_python

Deron Meranda deron.meranda at gmail.com
Thu Aug 10 17:37:27 EDT 2006


[Re-forwarding back on the list ... this may be useful to others too]

On 8/10/06, Nando Drabik <nandogringo at gmail.com> wrote:
> Many, many thanks ...
>
> So, I can run python scripts from the webserver in 2 different ways,
> one way is to use CGI and the other is to use mod_python right? I was
> confused because I thought the only way to run python code was by
> using mod_python.

There may be more ways too, but either CGI or mod_python are
by far the two most common ways to call python from Apache.

> You hit it on the spot, Deron. My goal is to send a serialized json
> object with XMLHttpRequest in the background of the html page and have
> a python script process it, fetch some data and send it back to the
> html for rendering.

You can't use FieldStorage for JSON; but that certainly doesn't
mean you can't or shouldn't use JSON.  You just need to parse
the JSON encoded data directly, rather than letting the FieldStorage
class attempt to do it.  Easy enough.  And it's always best to put
the complexity on the Python side if necessary and keep the
Javascript side as simple as possible; so JSON is perfect for that.

> If it's not asking too much, I'd like to ask your opinion. From what I
> understand, after reading some more, mod_python is more efficient than
> CGI modules and it seems that I can accomplish my goals with
> mod_python alone. Would there be any advantage of using CGI *and*
> python then? Sorry if this is a newbie question, but hey.. when it
> comes to cgi and python, I am one.

Whether to use CGI or mod_python is really a call you have to make.

CGI is perhaps quite a bit simpler and fool-proof, and so certainly
something that a "beginner" should not regret using.  mod_python
does have a lot more power than CGI (as it is more tightly integrated
into the internal hooks in Apache); but for simple request/response
processing you don't really need that.

Perhaps the biggest advantage of CGI is that it almost always works
out-of-the box.  mod_python is an add-on to Apache and thus has to
be compiled and configured into your Apache runtime.  This could
require no work (such as with most Linux distributions), or could
even be impossible for you to do yourself if you're not an administrator
(say with an ISP hosting facility).

mod_python can have a lot better performance, but the trade off
is complexity, or at least more to learn.  If you don't need performance
now, just continue using CGI until you get things completely working.
You can always convert over to mod_python after you've gained a bit
of experience (and time).

However that being said, I don't think mod_python is that hard
to use; so don't let this scare you off if you're interested and
willing to learn.
-- 
Deron Meranda


More information about the Mod_python mailing list