listing26-8.py
来自「《Beginning Python--From Novice to Profes」· Python 代码 · 共 53 行
PY
53 行
#!/usr/bin/pythonprint 'Content-type: text/html\n'import cgitb; cgitb.enable()def quote(string): if string: return string.replace("'", "\\'") else: return stringimport psycopgconn = psycopg.connect('dbname=foo user=bar')curs = conn.cursor()import cgi, sysform = cgi.FieldStorage()sender = quote(form.getvalue('sender'))subject = quote(form.getvalue('subject'))text = quote(form.getvalue('text'))reply_to = form.getvalue('reply_to')if not (sender and subject and text): print 'Please supply sender, subject, and text' sys.exit()if reply_to is not None: query = """ INSERT INTO messages(reply_to, sender, subject, text) VALUES(%i, '%s', '%s', '%s')""" % (int(reply_to), sender, subject, text)else: query = """ INSERT INTO messages(sender, subject, text) VALUES('%s', '%s', '%s')""" % (sender, subject, text)curs.execute(query)conn.commit()print """<html> <head> <title>Message Saved</title> </head> <body> <h1>Message Saved</h1> <hr /> <a href='main.cgi'>Back to the main page</a> </body></html>s"""
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?