user2216762
user2216762

Reputation: 11

accessing a database from a python cgi

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

Upvotes: 1

Views: 2577

Answers (1)

falsetru
falsetru

Reputation: 369264

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

import cgitb; cgitb.enable()

Upvotes: 1

Related Questions