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

    Join Date
    Dec 2004
    Posts
    9
    Rep Power
    0

    cgi.fieldstorage help


    I'm trying to learn python but am having problems with this code, it is from a book I bought on python, and the first cgi tutorial I try doesn't work. Basically you submit a form to it and it's supposed to display the information but everytime I try it I get an internal server error, I know Python is working on my server because other scripts run, any help would be greatly appreciated.


    The problem
    PHP Code:
    #!/usr/bin/python
    import cgi
    reshtml 
    '''Content-Type: text/html\n
    <HTML><HEAD><TITLE>
    Friends CGI Demo (dynamic screen)
    </TITLE></HEAD>
    <BODY><H3>Friends list for: <I>%s</I></H3>
    Your name is: <B>%s</B><P>
    You have <B>%s</B> friends.
    </BODY></HTML>'''
    form cgi.FieldStorage()
    who form['person'].value
    howmany 
    form['howmany'].value
    print reshtml % (whowhohowmany
    I can get HTML to print fine, but when I add in the (who = form['person'].value) lines, nothing works.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    Code:
    who = form['person'].value
    howmany = form['howmany'].value
    Unless you have html inputs with the names 'person' and 'howmany' cgi.FieldStorage() will return nothing. If you had something like:

    Code:
    <form action="index.html" method="post">
    <input maxlength="40" name="person" />
    <input maxlength="40" name="howmany" />
    <input value="Go" type="submit"
    </form>
    Then if index.html gets called with information in the 'person' and 'howmany' fields, cgi.FieldStorage() will return a dictionary like {'person': 'bob', 'howmany':'10'} or whatever values you entered.

    Basically, if their is no html inputs with the approprate names then cgi.FieldStorage can't identify their values.
    I'll learn this stuff someday.
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    It is commonly a good idea to check the fields before trying to access the values this is true for all web development languages I've used you can do this using pythons in keyword. For more information check out Preston Landers and my article on Python & CGI in Devsheds Python section.

    http://www.devshed.com/c/a/Python/Writing-CGI-Programs-in-Python/
    http://www.devshed.com/c/a/Python/Python-on-the-Web/

    Hope this helps,

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

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    9
    Rep Power
    0
    I forgot to mention but I do have a form that sends that data 'person' and 'howmany', the problem is after I sumbit and go to the form the page is blank, it is not printing anything, any ideas on what may cause that, or is there a problem with this code?
  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Originally Posted by billgrande
    I forgot to mention but I do have a form that sends that data 'person' and 'howmany', the problem is after I sumbit and go to the form the page is blank, it is not printing anything, any ideas on what may cause that, or is there a problem with this code?
    Code:
    import cgitb; cgitb.enable()
    Try the above. If you are getting an exception it should show it to you.

    BTW It was not clear to me from your other posts that python cgi was actually working at all - is it? Does a simple hello world script work?

    Is anything reported in your apache error log?

    grim
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    9
    Rep Power
    0
    Thanks for all the suggestions so far, but I still can't get this simple thing to work. A hello world does work and other python scripts work so I know python is working correctly, and permission are set to 755.When I tried to put
    Code:
    import cgitb; cgitb.enable()
    into my script it gave an internal server error, I looked at the apache error logs and it shows "Premature end of script headers" when trying to access that file. I still can't figure out why this is not working, any more ideas?

    Bill
    Last edited by billgrande; May 16th, 2005 at 08:23 PM. Reason: Addition
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    cgitb should be available to you - what error did you get?

    In the link Netytan posted:
    http://devshed.spunge.org/Server_Side/Python/CGI/page4.html
    there is a general python cgi script for displaying form elements.
    Does that work for you?

    grim
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    It looks to me like your Content-Type header is out of whack. You only have one newline character in there and you need to leave a empty line between the MIME headers and the HTML content.

    I would suggest printing your headers separately, this saves confusion and makes sure that the headers are sent first. It also makes things easier to follow .

    Hope this helps,

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


IMN logo majestic logo threadwatch logo seochat tools logo