listing26-6.py 878 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/python
  2. print('Content-type: text/html\n')
  3. import cgitb; cgitb.enable()
  4. import psycopg2
  5. conn = psycopg2.connect('user=foo password=bar dbname=baz')
  6. curs = conn.cursor()
  7. import cgi, sys
  8. form = cgi.FieldStorage()
  9. id = form.getvalue('id')
  10. print("""
  11. <html>
  12. <head>
  13. <title>View Message</title>
  14. </head>
  15. <body>
  16. <h1>View Message</h1>
  17. """)
  18. try: id = int(id)
  19. except:
  20. print('Invalid message ID')
  21. sys.exit()
  22. curs.execute('SELECT * FROM messages WHERE id = %s', (format(id),))
  23. rows = curs.dictfetchall()
  24. if not rows:
  25. print('Unknown message ID')
  26. sys.exit()
  27. row = rows[0]
  28. print("""
  29. <p><b>Subject:</b> {subject}<br />
  30. <b>Sender:</b> {sender}<br />
  31. <pre>{text}</pre>
  32. </p>
  33. <hr />
  34. <a href='main.cgi'>Back to the main page</a>
  35. | <a href="edit.cgi?reply_to={id}">Reply</a>
  36. </body>
  37. </html>
  38. """.format(row))