[Tutor] Python CGI Script

Kent Johnson kent37 at tds.net
Wed Sep 20 14:05:14 CEST 2006


Faran wrote:
> I Have a CGI Script Which is working perfectly when run from the python 
> interpreter, i m using the Content-type: 
> application/x-www-url-form-encoded , i use it to send data from flash 
> apps to python script. i checked the script with content-type: text/html 
> , and browsers printed the output perfectly, but when i use the 
> application content type, it gives the error, normally , firefox just 
> prints everything, so i dont know whats wrong. heres the script, i m 
> using the M ySQLdb for the Database Connection. Why isnt it Working?

It seems a bit unusual to use that content type to return data to the 
browser, it is usually used for form submissions. I'm not sure why it 
doesn't work but I have a couple of note below.
> 
> import MySQLdb as sql
> import cgi,cgitb
> 
> cgitb.enable()
> 
> class Listing:
>     def __init__(self):
> 
>         form = cgi.FieldStorage()
>         self.DBid = form.getvalue("DBid")
>         self.tableid = form.getvalue("tableid")
>         self.rangeid1 = form.getvalue("StartRange")
>         self.rangeid2 = form.getvalue("EndRange")
>        
>         conn = sql.connect('localhost','root','xxxxxxx',db=self.DBid)
>         self.cursor = conn.cursor()
>         self.conn = conn
>         self.list1 = []
>         self.list2 = []
>         self.list3 = []
>         self.list4 = []
>         self.list5 = []
>         self.list6 = []
>         self.outputstring = ""
> 
>     def listquery(self):
>         query1 = """SELECT ABC FROM %s limit %s,%s"""\
>                       % (self.tableid,self.rangeid1,self.rangeid2)
>         query2 = """SELECT DEF FROM %s limit %s,%s"""\
>                      % (self.tableid,self.rangeid1,self.rangeid2)
>         query3 = """SELECT GHI FROM %s limit %s,%s"""\
>                     % (self.tableid,self.rangeid1,self.rangeid2)
>         query4 = """SELECT JKL FROM %s limit %s,%s"""\
>                     % (self.tableid,self.rangeid1,self.rangeid2)
>         query5 = """SELECT MNO FROM %s limit %s,%s"""\
>                        % (self.tableid,self.rangeid1,self.rangeid2)
>         query6 = """SELECT PQR FROM %s limit %s,%s"""\
>                         % (self.tableid,self.rangeid1,self.rangeid2)
> 
>         self.list1 = self.queryexecute(query1)
>         self.list2 = self.queryexecute(query2)
>         self.list3 = self.queryexecute(query3)
>         self.list4 = self.queryexecute(query4)
>         self.listt5 = self.queryexecute(query5)
>         self.list6 = self.queryexecute(query6)
> 
>     def queryexecute(self,query):
>         templist = []
>         self.cursor.execute(query,)
>         for a in self.cursor.fetchall():
>             templist.extend(a)
> 
>         return templist
>     def outputappend(self,listtoappend,appname):
>         tempstring = ""
>         for a in range(0,len(listtoappend)):
>             tempstring += appname + str(a+1) + "x" + "=" +\
>                           listtoappend[a] + "&"

You should call urllib.quote_plus(listtoappend[a]) to make sure special 
characters are correctly escaped.

>         return tempstring
>    
>     def output(self):
>        
>         self.outputstring += self.outputappend(self.list1,"list1")
>         self.outputstring += self.outputappend(self.list2,"list2")
>         self.outputstring += self.outputappend(self.list3,"list3")
>         self.outputstring += self.outputappend(self.list4,"list4")
>         self.outputstring += self.outputappend(self.list5,"list5")
>         self.outputstring += self.outputappend(self.list6,"list6")
>         print """Content-type: application/x-www-url-form-encoded\n"""

You should have '\r\n\r\n' after the header, not just '\n'.

>         print """%s""" % (self.outputstring)

Could just be
   print self.outputstring

HTH,
Kent

>        
>     def clear(self):
>         self.cursor.close()
>         self.conn.close()
> 
> x = Listing()
> x.listquery()
> x.output()
> x.clear()
>        
>        
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 




More information about the Tutor mailing list