Thread: Python CGI

    #1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24

    Python CGI


    I'm new to using Python on the internet. To make HTML files, using Python stuff in the files, etc., etc. So please excuse any mistakes I use in my wording. A simple correction will do.

    http://24.125.59.73/tests/testfile.py

    Is it possible to have a form, and what you put in the text area, will be the raw_input. You then tell Python what to do with it, and display it on a page.

    If it is possible, can someone show me how I can do it?

    Lets use the following example (found here):

    Code:
    print raw_input('Enter the word to be Reversed: ')[::-1]
    Netytan deserves credit for the code above. Thanks!
  2. #2
  3. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    12

    Lightbulb


    First you'll need a few lines of Javascript I believe, to handle fields and buttons. I leave that to you since my Javascript skills are t3h sucks.

    That being said, what you need is a base URL (like http://www.url.com/script.cgi) to which you'll pass arguments. Simply use a syntax like http://www.url.com/script.cgi?arg=blabla , where blabla is what the user entered in the field.

    Now Python comes into play. Use Netytan's code with this being put over it :
    Code:
    sys.stderr = sys.stdout
    form = cgi.FieldStorage()
    if form.has_key("arg"):
        the_arg = form.getvalue("arg")
        #put the rest of the code here
    else:
        #put a nice error message for your user
    And that's it. Don't forget to import cgi and sys !
    Time is the greatest of teachers ; sadly, it kills all of its students.
    - Hector Berlioz
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24
    Yeah, my Javascript skills aren't so great. I hate the language, and have never needed it.

    The program you are helping me make, can be found here:
    http://24.125.59.73/tests/testfile2.py

    Source code:
    Code:
    #!/usr/bin/env python
    
    print 'Content-Type: text/html\n'
    print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
    print '<html lang="en">'
    print ' <head>'
    print '  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'
    print '  <title>Python: Test Page 2</title>'
    print ' </head>'
    print ''
    print ' <body>'
    import cgi
    import sys
    sys.stderr = sys.stdout
    form = cgi.FieldStorage()
    if form.has_key("arg"):
        the_arg = form.getvalue("arg")
        print raw_input("Enter the word to be reversed: ")[::-1]
    else:
        print "The program did not execute correctly."
    print '<p> <a href="http://24.125.59.73/tests/testfile2.py?arg=test" title="?arg=test">?arg=test</a> </p>'
    print ' </body>'
    print '</html>'
    If you know Javascript, we'd really appreciate it if you helped us out with that part. Thanks!
    Last edited by MasterChief; February 19th, 2004 at 05:51 AM.
  6. #4
  7. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    12
    As I said, I really suck at Javascript - and BTW I absolutely can't access your HTTP server, sorry. But here's what a page MIGHT look like. With no Javascript involved

    Inside your <body> tags, add forms this way :
    Code:
    <form name="formname" action="text.py" method="post">
    <input type="text" size="20" name="inputname">
    <input type="submit" name="B1" value="Submit">
    </form>
    Not sure if the inputs' types need quotes, so try unquoting if it's not working. With this, your browser will take care of all syntax matters as to how to access your script ; if it doesn't work, try renaming it to .cgi.

    It's really late, I just hope I'm making sense.
    Time is the greatest of teachers ; sadly, it kills all of its students.
    - Hector Berlioz
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by SolarBear
    Now Python comes into play. Use Netytan's code with this being put over it :
    Code:
    sys.stderr = sys.stdout
    form = cgi.FieldStorage()
    if form.has_key("arg"):
        the_arg = form.getvalue("arg")
        #put the rest of the code here
    else:
        #put a nice error message for your user
    If you have Python 2.3 you can also check is the key exists in a dictionary using this (much) cleaner method. although for now the number of servers running Python 2.3 is kinda low, but for a localhost...

    Code:
    if 'arg' in form:
        the_arg = form.getvalue("arg")
    else:
        print 'Oops'
    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24
    http://24.125.59.73/tests/testfile2.py
    Here is an exact copy of my source code:
    testfile2.py
    Code:
    #!/usr/bin/env python
    
    print 'Content-Type: text/html\n'
    print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
    print '<html lang="en">'
    print ' <head>'
    print '  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'
    print '  <title>Python: Test Page 2</title>'
    print ' </head>'
    print ''
    print ' <body>'
    import cgi
    import sys
    sys.stderr = sys.stdout
    form = cgi.FieldStorage()
    if 'arg' in form:
        the_arg = form.getvalue("arg")
        print raw_input("Enter the word to be reversed: ")[::-1]
    else:
        print "The program did not execute correctly."
    print '<p> <a href="http://24.125.59.73/tests/testfile2.py?arg=test" title="?arg=test">?arg=test</a> </p>'
    print '  <form name="formname" action="testfile2.py" method="post">'
    print '   <input type="text" size="20" name="inputname">'
    print '   <input type="submit" name="B1" value="Submit">'
    print '  </form>' 
    print ' </body>'
    print '</html>'
    What now?

    (Thanks guys, for all of the help so far.)
    Last edited by MasterChief; February 20th, 2004 at 06:14 PM.
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hi Chief,

    you should note that using raw_input() in a webpage is a bad idea, often leading to an error... that is unless you have submitted a form using POST methods to that page in which case it displays a --------number string, nothing more .

    Why are you using the getvalue('key') method to access the value in you're form, since we've already desided that the form value exists (in the if statment) and you're not setting a default value anyway . Why not use this form.

    Code:
    >>> d = {'a': 1, 'b': 2, 'c': 3}
    >>> d['a']
    1
    >>> d['b']
    2
    >>>
    This is just a dictionary, in FieldStorage() you need to append .value to then end of you're dictionary what variable you want i.e.

    Code:
    >>> d['c'].value #Note don't try this with a normal dictionary :) or you'll get a nice error message; but works with FieldStorage()
    3
    Catch you latter guys.

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo