chat.py

This Python CGI script outputs the current list of comments

Try it:

script without a query
script with a query


01: #!/usr/bin/python
02: import MySQLdb
03: import dbname
04: import cgi, sys
05: import cgitb # CGI traceback module
06: cgitb.enable()
07: 
08: sys.stderr = sys.stdout
09: 
10: form = cgi.FieldStorage()
11: 
12: if (form.has_key('since')):
13:     where = 'WHERE modified > "%s"' % form['since'].value
14: else:
15:     where = ''
16: 
17: print "Content-Type: Text/plain"
18: print
19: 
20: db = dbname.dbopen();
21: if (not db): sys.exit(1)
22: cursor = db.cursor()
23: try:
24:     cursor.execute ('SELECT modified, nickname, message FROM users %s ORDER BY modified' % where )
25: except MySQLdb.Error, e:
26:     print e
27: 
28: rows = cursor.fetchall()
29: for row in rows:
30:     if (row[0]): modified = row[0]
31:     else: modified=''
32:     if (row[1]): nickname = row[1]
33:     else: nickname = ''
34:     if (row[2]): message = row[2]
35:     else: message = ''
36:     rs = (modified, nickname, message)
37:     print '"%s" "%s"  "%s"' % rs
38: cursor.close()
39: db.close()


Maintained by John Loomis, updated Tue Mar 04 23:05:53 2008