[mod_python] Help converting cgi to mod_python

Josh Goldsmith kd7spq at gmail.com
Thu Nov 18 10:00:43 EST 2004


Thank you Gustavo!  That's exactly what I was looking for!

-Josh


On Thu, 18 Nov 2004 08:42:18 -0600, Gustavo Córdova Avila
<gustavo.cordova at q-voz.com> wrote:
> Josh Goldsmith wrote: 
> 
> 
> Hi all,

 I'm sure that this is a very common question (though after
> looking
thru a few months of the archives maybe not) but is there an easy
> way
to convert a cgi written around the cgi module to work in mod_python?

> I've spent the last couple of weeks teaching myself python (and
MySQL) while
> writing a cgi and it is running slower than I'd like (15
tps on a 1ghz
> Athlon). In looking to speed it up, I observed that
starting Python was
> where most of the delay came from. With further
poking, I found mod_python
> and have been poking at it to get a feel
for it. Unfortunately I have not
> found nearly enough examples to
fully get it.

 Any pointers, links, or
> other help would be appreciated!

-Josh

PS. Using Python 2.3, Apache
> 2.0.52, and mod_python
> 3.1.3
_______________________________________________
Mod_python mailing
> list
Mod_python at modpython.org
http://mailman.modpython.org/mailman/listinfo/mod_python

> It's isn't much trouble converting a CGI to a mod_python handler, the ten
> points you need to know:
> 
>     1. At the top: from mod_python import apache, util
>     2. Encapsulate all your processing in functions, no inline-script stuff
> should do request processing.
>     3. Your program's entry point is a function called "handler" which takes
> a single parameter (the request object).
>     4. Some equivalencies:
>              environ["QUERY_STRING"]   --> request.args
>              environ["PATH_INFO"]   --> request.path_info
>              environ["HTTP_METHOD"] --> request.method
>              environ["CONTENT_LENGTH"] --> request.content_length
> 
>     5. For posted data, instead of stdin.read() do request.read()
>     6. To return data, instead of print or stdout.write(), do
> request.write()
>     7. Instead of cgi.FieldStorage(stdin) use util.FieldStorage(request)
>     8. Use request.content_type = "content/type" instead of printing it
> before the content.
>     9. Instead of printing out headers, use request.headers_out["name"] =
> "value"
>    10. Instead of looking for environ["HTTP_<header-name>"], use
> request.headers_in["header-name"]
> 
> Or, you could also use mod_python's cgi emulator and not have to do all
> this, but since you talk about maximizing performance, using a handler
> function is about 5 to 10 times quicker than using mod_python's builtin cgi
> emulator.
> 
> Good luck, hope this helps...
> 
> 
> -- 
> Gustavo Córdova Avila <gustavo.cordova at q-voz.com> 
> Tel: +52 (81) 8130-1919 ext. 127
> Integraciones del Norte, S.A. de C.V.
> Padua #6047, Colonia Satélite Acueducto
> Monterrey, Nuevo León, México.
> 
>



More information about the Mod_python mailing list