[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Running CGI from within CGI

rodmc

2/14/2008 2:17:00 PM

I am new to using Python as a CGI platform, so please excuse me if
this is a dumb question.

Anyway I have written a series of web forms (with Python scripts)
which allow the user input but also retrieve data from a database. The
data entry stage works fine however retrieving data is a little more
tricky. However I need to find a way to call the scripts directly from
within other CGI's. At present when I go to the next page I call the
normal HTML page as below:

p = open('somefile.html')
some = p.read()
p.close()
print some


However I would like to execute a script instead so calling for
example myscript.py - thus populating the existing forms with data.
This does not seem to work when I modify the above method. Is there
another way to do it? Thanks in advance.

Kind regards,

rod
3 Answers

Bruno Desthuilliers

2/14/2008 2:26:00 PM

0

rodmc a écrit :
> I am new to using Python as a CGI platform, so please excuse me if
> this is a dumb question.
>
> Anyway I have written a series of web forms (with Python scripts)
> which allow the user input but also retrieve data from a database. The
> data entry stage works fine however retrieving data is a little more
> tricky. However I need to find a way to call the scripts directly from
> within other CGI's. At present when I go to the next page I call the
> normal HTML page as below:
>
> p = open('somefile.html')
> some = p.read()
> p.close()
> print some
>
>
> However I would like to execute a script instead so calling for
> example myscript.py - thus populating the existing forms with data.
> This does not seem to work when I modify the above method. Is there
> another way to do it? Thanks in advance.

The QuickAndDirtyWay(tm) would be to use execfile IIRC. But since you
*don't* want to do it that way, the best thing to do would be to factor
out common code into functions in a distinct module (or in as many
modules as needed), then rewrite your cgi scripts so they import the
relevant modules and call the appropriate functions.

And while we're at it, I'd greatly recommand giving a look at the
various templating packages around (may I recommand Mako ?) and the
FormEncode package for HTTP forms conversion/validation ?

HTH

rodmc

2/14/2008 2:47:00 PM

0

Thanks for the details, is execfile full of security issues or
something?

You are right about exploring templates, I may explore that later.
However I need to integrate the forms in with another system, which
will not be using templates. I suspect I may have taken the long way
round but I suppose it also means I learn a little more Python :-)

Cheers,

rod

On Feb 14, 3:26 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.com> wrote:
> rodmc a écrit :
>
>
>
> > I am new to using Python as a CGI platform, so please excuse me if
> > this is a dumb question.
>
> > Anyway I have written a series of web forms (with Python scripts)
> > which allow the user input but also retrieve data from a database. The
> > data entry stage works fine however retrieving data is a little more
> > tricky. However I need to find a way to call the scripts directly from
> > within other CGI's. At present when I go to the next page I call the
> > normal HTML page as below:
>
> > p = open('somefile.html')
> > some = p.read()
> > p.close()
> > print some
>
> > However I would like to execute a script instead so calling for
> > example myscript.py - thus populating the existing forms with data.
> > This does not seem to work when I modify the above method. Is there
> > another way to do it? Thanks in advance.
>
> The QuickAndDirtyWay(tm) would be to use execfile IIRC. But since you
> *don't* want to do it that way, the best thing to do would be to factor
> out common code into functions in a distinct module (or in as many
> modules as needed), then rewrite your cgi scripts so they import the
> relevant modules and call the appropriate functions.
>
> And while we're at it, I'd greatly recommand giving a look at the
> various templating packages around (may I recommand Mako ?) and the
> FormEncode package for HTTP forms conversion/validation ?
>
> HTH



Bruno Desthuilliers

2/14/2008 3:34:00 PM

0

rodmc a écrit :
(top-post corrected - rod, please learn to quote, thanks !-)

> On Feb 14, 3:26 pm, Bruno Desthuilliers <bruno.
> 42.desthuilli...@wtf.websiteburo.oops.com> wrote:
>> rodmc a écrit :
(snip)
>>> However I would like to execute a script instead so calling for
>>> example myscript.py - thus populating the existing forms with data.
>>> This does not seem to work when I modify the above method. Is there
>>> another way to do it? Thanks in advance.
>
>> The QuickAndDirtyWay(tm) would be to use execfile IIRC. But since you
>> *don't* want to do it that way, the best thing to do would be to factor
>> out common code into functions in a distinct module (or in as many
>> modules as needed), then rewrite your cgi scripts so they import the
>> relevant modules and call the appropriate functions.

> Thanks for the details, is execfile full of security issues or
> something?

I don't think it has any peculiar problem wrt/ security, but it's plain
butt-ugly, and certainly a nightmare when it comes to readability and
maintainability.

FWIW, I never ever used execfile myself. My scripts (I mean, the .py
intended to be use as 'entry points', either on command line, CGI or
whatnot) have very few code at the top-level, almost only imports,
functions / classes / symbols definitions, and the canonical guard for
the effective entry point, ie:

# somescript.py
<imports here>
<globals and constants here>
<classes and/or functions here>

if __name__ == '__main__':
# been called as script, not imported as module, so let's proceed
<handle sys.args and whatnot>
<call relevant functions>
<output something>



HTH

(snip about templates, not relevant here)