Thread: Python & CGI

    #1
  1. ID10T
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Location
    Yes
    Posts
    632
    Rep Power
    27

    Python & CGI


    I want to use python with my webpage. In order to test it to see if it works i tried the following basic example:

    Code:
    <form action="cgi-bin/leapyear.py">
     <p>Enter a year and find out if it's a leap year:
     <input type="text" name="year" size="6">
     <input type="submit">
     <input type="reset">
    </form>
    with

    Code:
    #!/usr/bin/python
    import cgi
    
    def isLeap(year):
       if year % 400 == 0:
           return 1
       elif year % 100 == 0:
           return 0
       elif year % 4 == 0:
           return 1
       else:
           return 0
    
    reshtml = """Content-Type: text/html\n
    <html>
     <head><title>Leap Year</title></head>
    
     <body>
      <h1>Is it a leap year?</h1>
      <p>%s</p>
     </body>
    </html>"""
    
    form = cgi.FieldStorage()
    year = form['year'].value
    if isLeap(int(year)):
        message = "%s is a leap year." % year
    else:
        message = "%s is not a leap year." % year
    print reshtml % message
    Instead of running the code it just displayed it.

    So i checked with my hosting provider to see if CGI was available or if i had to put it in the CGI-BIN directory to which i dont have access and they responded saying that yes it is available and i can run it from any folder - it doesnt have to be in CGI-BIN.


    But that changes nothing because it still doesnt work. I have never done this before so where could i be going wrong?

    Thanks!


    Edit: leapyear.py is in the same folder as the webpage and action = 'xx' has been changed accordingly.

    I also chmodded everything to rwx for all users - no dice.
    Last edited by Civ247; February 28th, 2006 at 04:23 PM.
  2. #2
  3. ID10T
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Location
    Yes
    Posts
    632
    Rep Power
    27
    ok i tried a different simpler example and named it test.cgi instead of test.py

    Code:
    #!/usr/bin/python
    
    # Required header that tells the browser how to render the text.
    print "Content-Type: text/plain\n\n"
    
    # Print a simple message to the display window.
    print "Hello, World!\n"
    I get the following error:

    The server encountered an internal error or misconfiguration and was unable to complete your request.
    Any ideas?
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    330
    Rep Power
    15
    I was dealing with that problem myself for almost a month last year and I gave up. It seems that most service providers don't support Python in their cgi-bin and you can't put anything in that bin yourself either, which again I tried myself. You have a better chance with Perl ofcourse, since for historical reasons it is more supported by servers.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Maybe their copy of Python isn't installed in /usr/bin/python ...

    Email them and ask - or try the "#!/bin/env python" format.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    53
    Rep Power
    11
    do you have shell access to the server? if so, 'which python' will give you the location. but /usr/bin/env python should work. also, check the permissions on the script. it needs read and execute for everyone, since you're not sure what user the server is running the script as. could be set up as your user (probably not), nobody, www, www-data, etc, etc. could be anything, so 766 perms would be good.
  10. #6
  11. ID10T
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Location
    Yes
    Posts
    632
    Rep Power
    27
    thanks for the tips. Python is definitley in /usr/bin/python - they told me that. Unfortunately i dont have shell access.

    I wrote a program that scans all files in a web project and counts different types of files and counts lines of code, comments, image files etc

    i did it in python because i knew it would take me twice as long in php.

    the project is due on soon so im just going have to do it in php.

    Thanks for the help
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    the project is due on soon so im just going have to do it in php.
    I don't understand why you aren't asking your web host's support about this instead. Your simple example of printing hello world looks like it should work fine...
  14. #8
  15. ID10T
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Location
    Yes
    Posts
    632
    Rep Power
    27
    Originally Posted by sfb
    I don't understand why you aren't asking your web host's support about this instead. Your simple example of printing hello world looks like it should work fine...
    yea i did that first & they were pretty good & responsive. i think the guy that answered wasnt aware of the fact that python doesnt work. I thought i had some bug in the code and didnt want to bug the hosters about that

IMN logo majestic logo threadwatch logo seochat tools logo