Separation of Code in CGI App

Discussion in 'Python' started by Rob Cowie, Jul 22, 2005.

  1. Rob Cowie

    Rob Cowie Guest

    Hi,

    I need to create a planner/calendar system using python cgi scripts. It
    is my first CGI app (beyond a few tutorial examples).

    I wish to separate the code according to function. For instance, the
    code to handle the DB connection, SQL querying, HTML form variable
    retrieval, rendering of HTML via some sort of template system etc. Each
    will be a class instantiated when the CGI app is called.

    To do this, do I just create each of these classes in one, monolithic
    ..cgi file? or is it possible to separate these out into different
    scripts and have a 'controller' script call them and pass the relevant
    parameters/data? I guess I could also store the python code in text
    files and use the 'execfile()' function, although it seems to me that
    would be an uneccasary performance hit.

    If that is possible, is that the right way to go? If not, suggestions
    would be gratefully recieved!
    Rob Cowie, Jul 22, 2005
    #1
    1. Advertising

  2. Rob Cowie enlightened us with:
    > I need to create a planner/calendar system using python cgi scripts.
    > It is my first CGI app (beyond a few tutorial examples).


    Are you sure you want to use CGI? I'd go for mod_python instead if at
    all possible, since that'll give you a much better performance.

    > To do this, do I just create each of these classes in one,
    > monolithic .cgi file?


    Please don't. It's much better to put things into modules, and import
    them when needed.

    > or is it possible to separate these out into different scripts and
    > have a 'controller' script call them and pass the relevant
    > parameters/data? I guess I could also store the python code in text
    > files and use the 'execfile()' function, although it seems to me
    > that would be an uneccasary performance hit.


    Why that difficult? What's wrong with just doing "from htmlgenerator
    import pagexy"?

    > If that is possible, is that the right way to go? If not,
    > suggestions would be gratefully recieved!


    Check out http://www.unrealtower.org/mycheetah

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
    Sybren Stuvel, Jul 22, 2005
    #2
    1. Advertising

  3. Rob Cowie

    Rob Cowie Guest

    So, do I separate out whatever code I want to into .py modules, then
    have the cgi script import them, and utilise them as required?

    If so, do I need to put these modules somewhere described in the System
    Path? Can I modify the PATH at runtime to point to my modules? How?

    Cheers for being patient!
    Rob Cowie, Jul 22, 2005
    #3
  4. Rob Cowie

    Paul McNett Guest

    Rob Cowie wrote:
    > So, do I separate out whatever code I want to into .py modules, then
    > have the cgi script import them, and utilise them as required?


    Bingo!


    > If so, do I need to put these modules somewhere described in the System
    > Path?


    Put them in the same directory, and you don't have to worry any more
    about it. You can get more advanced if you want by putting your modules
    into a "package", which is just a directory with a __init__.py file -
    see the docs at python.org.


    > Can I modify the PATH at runtime to point to my modules? How?


    If you want to have them in a different directory, and the directory
    isn't already in sys.path, then just issue, from your main script,
    before your import statements:

    import sys
    sys.path.insert(0, "/path/to/your/modules")


    > Cheers for being patient!


    Welcome to Python! Being new to it, you may want to consider having
    someone review your code before putting it on the public internet,
    simply because there are lots of opportunities for inadvertantly
    providing backdoors (Python is powerful). This is especially true if you
    let your users type into a text box, or if you do dynamic database
    queries, or both.

    Also, have you run through the tutorial on python.org?


    --
    Paul McNett
    http://paulmcnett.com
    Paul McNett, Jul 22, 2005
    #4
  5. Rob Cowie

    Rob Cowie Guest

    Thanks,

    I have run through the tutorial - very good. One day, I'd like to try
    and organise a similar tutorial involving CGI programming. There are
    several very basic examples, but they leave plenty of unanswered
    questions such as how to best structure a cgi app, apache permissions,
    etc

    So to be clear, if a .py module is in the same directory as my python
    cgi script, I can import it thus 'import XXX.py'?
    Rob Cowie, Jul 22, 2005
    #5
  6. Rob Cowie

    Paul McNett Guest

    Rob Cowie wrote:

    > So to be clear, if a .py module is in the same directory as my python
    > cgi script, I can import it thus 'import XXX.py'?


    Not quite. If you had a XXX.py, XXX.pyc, and/or XXX.pyo in the same
    directory as your Python cgi script, you'd import it by issuing:

    import XXX


    --
    Paul McNett
    http://paulmcnett.com
    Paul McNett, Jul 22, 2005
    #6
  7. Rob Cowie a écrit :
    > Hi,
    >
    > I need to create a planner/calendar system using python cgi scripts. It
    > is my first CGI app (beyond a few tutorial examples).
    >

    (snip)

    You may want to have a look at the cgi_app mini-framework:
    -> http://thraxil.org/code/cgi_app/

    It's a port of a Perl framework, and it has some unpythonic flavour in
    some points (but that can be easily corrected), and it can work with CGI
    and mod_python. It's quite easy to get started with, and the source
    being really small (about 1kloc IIRC), it's quite easy to customize to
    your own needs (I rolled my own mod_python-only/SimpleTAL-only version
    in half a day...).

    You also have the choice between 2 template systems, one that's dirty as
    can be IMHO (htmltmpl) and one that is quite nice (SimpleTAL). The only
    wart with SimpleTAL is that it requires every input to be unicode, but
    that's easy to solve also (I wrote a wrapper class that handle this
    quite transparently - it's still Q&D but works well so far, I can
    publish it if anyone is interested).

    My 2 cents
    Bruno Desthuilliers, Jul 23, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wayne Liu

    Question on view and code separation

    Wayne Liu, Jul 25, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    338
    Wayne Liu
    Jul 25, 2003
  2. tshad
    Replies:
    2
    Views:
    343
    tshad
    Dec 2, 2004
  3. Michael

    Code-separation in ASP.NET 2.0

    Michael, Nov 11, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    694
    Juan T. Llibre
    Nov 12, 2005
  4. epsilon
    Replies:
    4
    Views:
    314
    Gabriel Genellina
    Aug 22, 2007
  5. prschmid

    Data and Code separation

    prschmid, Jul 25, 2004, in forum: ASP General
    Replies:
    5
    Views:
    150
    Jeff Cochran
    Jul 25, 2004
Loading...

Share This Page