1

I configured the httpd file to run python scripts as given in a website. After the configuration I was amazed to see .py file getting executed when placed in the htdocs folder, but .cgi files are not being executed. The error says an internal error. Thought of proceeding with .py files but when I try to access mysql database, I am not able to. My .py file is:

import cgi
import MySQLdb

print "Content-type: text/html"
print
print "<html><head>"
print ""
print "</head><body>"
form=cgi.FieldStorage()
name=form["t1"].value
print "Hello. %s" %name
print "hai"
print '<input type="submit"/>'
Con = MySQLdb.Connect(host="127.0.0.1", port=3306, user="root", passwd="pwd", db="db1")
cursor = Con.cursor()
sql="SELECT * FROM rec"
cursor.execute(sql)
data = cursor.fetchone()
print "%s" %data
print "</body></html>"

I am not getting any error, but 'data' is not getting printed output I got was:

hello name hai submit button

new to python. So can u guys please help me out? python version-2.7 db-mysql server-apache 2.2 win32 bit

3
  • 1
    you probably are getting an error, but you won't see that in the output, as it ends up in apache's log, so check your logs, or add cgitb: import cgitb; cgitb.enable() Commented Jun 13, 2013 at 16:51
  • You might not want to connect as root, try making a user account that does not have the permission the delete your whole database. It's never the best idea to use an admin mode as user mode. Commented Jun 13, 2013 at 17:23
  • when i try to connect to the db using python, i get it right...but in python cgi its not working.... Commented Jun 13, 2013 at 17:40

1 Answer 1

1

Put following line at first line, and see what error happened (traceback).

import cgitb; cgitb.enable()
Sign up to request clarification or add additional context in comments.

7 Comments

yes...it shows an error,..internal server error..can u please tell y is this error showing up,,is there any mistake with the code?
Does not the page display any traceback?
If you don't traceback, try cgi script in command line: echo t1=blah | python cgi_script.py
yaa..i did add #!/python27/python/ forgot to put up in the query...sorry
hey.. i didnt get the output...help me solve the problem,besause i am stuck in the middle of a project
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.