Thread: Python CGI

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

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10

    Python CGI


    ok whats up guys i have another problem and a few questions:

    how to get python cgi to work with sql..so i can gather basic info ?

    what causes the scripts to show the source code..when its being "ran"?

    ok my problem is that everytime i try to run my script "example.cgi" n i put it into the cgi-bin/ folder apache wont run the script for some odd reason ty in advance
  2. #2
  3. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    20
    Are sure you have everything set up correctly? Maybe this might help in verifying that.
    Am I supposed to sign here?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    i dont kno what is goin on everytime a try to set up the damn form:
    <form method type="post" action="http://my_site_/cgi-bin/test.py>

    ok when i press send it loads the damn source into the browser instead of running the script itself

    does apache have to be config to those setings..or what
  6. #4
  7. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    20
    Of course it has to be configured... Have you checked out the link above?
    Am I supposed to sign here?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    yes i did..but i may have done it wrong im not sure but the test.cgi or test.py wont run properly

    Code:
    import cgi
    
    def generate_html():
       print '<html>\n'
       print '<title>testing cgi script</title>\n'
       print '<body>\n'
       print '<form method="post" action="test.cgi">\n'
       print '<p>First Name: <input type="text" name="firstname">\n'
       print '<p>Last Name: <input type="text" name="lastname">\n'
       print '<p><input type="submit" value="send">\n'
       print '</form>\n'
       print '<body>\n'
       print '</html>\n'
    
    def main():
       print 'Content-Type: text/html\n'
       form = cgi.FieldStorage()
       if form.has_key('firstname') and form['firstname'].value != '':
          print '<h1>Hello', form['firstname'].value, '</h1>'
    
    main()


    perhaps u can tell me whats wrong wit my http file or copy and passte yours herre
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    WA
    Posts
    29
    Rep Power
    0
    Here's a sample from my form and the cgi that it posts to.

    Form:
    Code:
    <form method="POST" action="http://domain.com/cgi-bin/register.py">
    Username: <input type="text" name="username"><br>
    Password: <input type="password" name="password"><br>
    </form>

    Here's the sql stuff plus some form handling

    Code:
    #!/usr/bin/python
    import cgi,os,MySQLdb,md5,sys
    
    print "Content-type: text/html\n\n"
    form = cgi.FieldStorage()
    uu = form["username"].value
    u = MySQLdb.escape_string(uu)
    p = form["password"].value
    conn = MySQLdb.connect(host = "localhost", user = "root", passwd = "********", db = "test")
    cursor = conn.cursor()
    query = "SELECT COUNT(*) FROM logincreds WHERE username='%s'" % ( u )
    cursor.execute(query)
    numentries = cursor.fetchall()
    if cursor.fetchall() == "((0L,),)":
            try:
                    add = "cursor.execute(\"INSERT into logincreds (username, password) VALUES ('" + u $
                    print add
                    #print "<br>" + blah
                    exec(add)
            except MySQLdb.Error, e:
                    print "Error %d: %s" % (e.args[0], e.args[1])
                    sys.exit (1)
    else:
            print "blah"

    I don't currently see an issue with yours, but I hope mine helps you
    It seems like you
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    What happens exactly when you try to run the script by fetching the URL? Does it time out? Return a 500 error? A 404? Show the python source? Can you access the apache error logs, and if so what do they say? Without all this information it is going to be impossible for anyone to diagnose what is going wrong.

    Anyway, here are some common problems that you could check for:

    1) python not being installed where you think it is - your script starts with #!/usr/bin/python - are you absolutely sure that is where it is installed? It can vary between different version of Linux.

    2) the permissions not being set on the file. It needs to be executable by the apache process.

    3) apache not set up to run CGI files from that directory or with that extension. You will need to check with the service provider.

    4) CRLF issues. If the file has been edited on Windows and has carriage returns in it, then it will fail to execute on Linux.

    This list in not exhaustive, but covers most of the problems I have encountered in the past.

    Dave
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    basically the script will schow the source code of file but not actually run...is there a way to c a http config file that runs python cgi perhaps a link or sumthing
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    51
    Rep Power
    10
    Alright if i remeber correctly there are a few things must be done in order to configure apache...

    Find this line in your apache config file (httpd.conf)

    Code:
    AddHandler cgi-script .cgi
    Change it to
    AddHandler cgi-script .cgi .py
    This isnt neccessary but it allows you to keep the extension .py when you run the file...

    Next find a line that looks something like
    Code:
    Options Indexes FollowSymLinks
    Change to:
    Options Indexes FollowSymLinks ExecCGI

    Your first line of code must always be this... The shebang line in any python program

    Code:
    #!C:/Python24/python.exe
    Unless you are hosting it on a unix machine, im not sure the shebang line then. You will have to look it up. If this doesn't work ill upload my configuration file. HTH
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    in order for the python code to run correctly do i need to have it hooke up to a mysql or would it be possible to test it with jus the python code(import cgi) and apache.

    if possible where can i get a decent tutorial on cgi programming with python, as close as possible to a "this is for newbs/step-by-step".

    thank you for your post.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    51
    Rep Power
    10
    You should first just test if python even works.. Start a new python file and name it what you would like. Then insert this code.
    Code:
    #!C:/Python24/python.exe
    print "Content-type: text/html\n\n"
    print "hey"
    If it comes up and just says hey then python worked. Then you can now worrying about configuring cgi.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    no actually its doin the same thing i been discribing in the last few post...still shows the whole source of python....i tried that link in the thread but i still cudnt get it to work...do i have to download something or jus instert the line of txt it tells me 2?
  24. #13
  25. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    It appears that your webserver is not configured correctly. You did restart apache after editing httpd.conf, right??
    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
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    Originally Posted by Scorpions4ever
    It appears that your webserver is not configured correctly. You did restart apache after editing httpd.conf, right??
    yess I did umm I believe u are correct on this, but i am unaware of how to configure it properly.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    im tired of this crap it wont work....i tried tht mod python too and i get a loading error on that....is it possible for sum1 send me their apache file already fingured for python and php. then i can compare it to mine and fix the problem.

    post back if u need my email to send it to me

IMN logo majestic logo threadwatch logo seochat tools logo