listing26-6.py

来自「《Beginning Python--From Novice to Profes」· Python 代码 · 共 48 行

PY
48
字号
#!/usr/bin/pythonprint 'Content-type: text/html\n'import cgitb; cgitb.enable()import psycopgconn = psycopg.connect('dbname=foo user=bar')curs = conn.cursor()import cgi, sysform = cgi.FieldStorage()id = form.getvalue('id')print """<html>  <head>    <title>View Message</title>  </head>  <body>    <h1>View Message</h1>    """try: id = int(id)except:    print 'Invalid message ID'    sys.exit()curs.execute('SELECT * FROM messages WHERE id = %i' % id)rows = curs.dictfetchall()if not rows:    print 'Unknown message ID'    sys.exit()row = rows[0]print """    <p><b>Subject:</b> %(subject)s<br />    <b>Sender:</b> %(sender)s<br />    <pre>%(text)s</pre>    </p>    <hr />    <a href='main.cgi'>Back to the main page</a>    | <a href="edit.cgi?reply_to=%(id)s">Reply</a>  </body></html>""" % row

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?