Thread: python, cgi?

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    1
    Rep Power
    0

    python, cgi?


    Hi,

    New here to Python. Need help:

    Wrote a code "test.py", so when run it command line, generate a file "outfile" which is a html page.

    Q: how to do so in a web page, to run the code when url is directed? Need cgi?

    Web server is a Linux system.

    Thanks.

    M
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    yeh, you'll need to configure apache (I'm assuming you're using apache, but if not then whatever http server you're using) to execute python scripts with your python install. I'm not sure how well you know your way around your web server, but if you are indeed on apache then the documentation on their site is all you need: http://httpd.apache.org/
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    Also, you'll need to change your script to print an appropriate response header and send your output to stdout, instead of to a file.

    For example:
    Code:
    # Print a header and a blank line afterwards to indicate end of header
    print "Content-type: text/html"
    print
    
    # Now print your HTML page
    print "<html><head><title>Hello</title></head>"
    print "<body>"
    print "   Hello World"
    print "</body>"
    print "</html>"
    Also, to make your CGI script handle form variables, you may wish to use the following modules:
    Code:
    import cgi
    # The next two lines are useful, to print script errors to the browser
    import cgitb
    cgitb.enable()
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo