Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 26 ■ ■ ■ Project 7: Your Own Bulletin Board Many kinds of software enable you to communicate with other people over the Internet. You’ve seen a few already (for example, the Usenet groups in Chapter 23 and the chat server in Chapter 24), and in this chapter you will implement another such system: a Web-based discussion forum. What’s the Problem? In this project, you create a simple system for posting and responding to messages via the Web. This has utility in itself, as a discussion forum. One famous example of such a forum is Slashdot (http://slashdot.org). The system developed in this chapter is quite simple, but the basic functionality is there, and it should be capable of handling quite a large number of postings. However, the material covered in this chapter has uses beyond developing stand-alone discussion forums. It could be used to implement a more general system for collaboration, for example, or an issue tracking system, or something completely different. The combination of CGI (or similar technologies) and a solid database (in this case, an SQL database) is quite powerful and versatile. ■Tip Even though it’s fun and educational to write your own software, in many cases it’s more cost effective to search for existing software. In the case of discussion forums and the like, chances are that you can find quite a few well-developed systems freely available already. Specific Goals The final system should support the following: • Displaying the subjects of all current messages • Message threading (displaying replies indented under the message they reply to) • Viewing existing messages 483
484 CHAPTER 26 ■ PROJECT 7: YOUR OWN BULLETIN BOARD • Posting new messages • Replying to existing messages In addition to these functional requirements, it would be nice if the system was reasonably stable, could handle a large number of messages, and avoided such problems as two users writing to the same file at the same time. The desired robustness can be achieved by using a database server of some sort, instead of writing the file-handling code yourself. Useful Tools In addition to the CGI stuff from Chapter 15, you’ll need an SQL database, as discussed in Chapter 13. You could either use the stand-alone database SQLite, which is used in that chapter, or you could use some other system, such as either of the following two excellent, freely available databases: • PostgreSQL (http://www.postgresql.org) • MySQL (http://www.mysql.org) In this chapter, I use PostgreSQL for the examples, but the code should work with most SQL databases (including MySQL) with few edits. Before moving on, you should make sure that you have access to an SQL database server and check its documentation for instructions on how to manage it. In addition to the database server itself, you’ll need a Python module that can interface with the server (and hide the details from you). Most such modules support the Python DB-API, which is discussed in more detail in Chapter 13. In this chapter, I use psycopg (http://initd.org/ Software/psycopg), a robust front-end for PostgreSQL. If you’re using MySQL, the MySQLdb module (http://sourceforge.net/projects/mysql-python) is a good choice. After you have installed your database module, you should be able to import it without raising any exceptions. For psycopg: >>> import psycopg >>> For MySQLdb: >>> import MySQLdb >>> You get the idea. Preparations Before your program can start using your database, you have to actually create the database. That is done using SQL (see Chapter 13 for some pointers on this). The database structure is intimately linked with the problem and can be a bit tricky to change once you’ve created it and populated it with data (messages). Let’s keep it simple: You’ll have only one table, which will contain one row for each message. Each message will have a unique ID (an integer), a subject, a sender (or poster), and some text (the body).
- Page 462 and 463: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 464 and 465: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 466 and 467: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 468: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 471 and 472: 440 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 473 and 474: 442 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 475 and 476: 444 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 477 and 478: 446 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 479 and 480: 448 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 481 and 482: 450 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 483 and 484: 452 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 486 and 487: CHAPTER 24 ■ ■ ■ Project 5: A
- Page 488 and 489: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 490 and 491: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 492 and 493: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 494 and 495: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 496 and 497: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 498 and 499: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 500 and 501: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 502 and 503: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 504 and 505: CHAPTER 25 ■ ■ ■ Project 6: R
- Page 506 and 507: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 508 and 509: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 510 and 511: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 512: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 517 and 518: 486 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 519 and 520: 488 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 521 and 522: 490 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 523 and 524: 492 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 525 and 526: 494 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 527 and 528: 496 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 529 and 530: 498 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 531 and 532: 500 CHAPTER 27 ■ PROJECT 8: FILE
- Page 533 and 534: 502 CHAPTER 27 ■ PROJECT 8: FILE
- Page 535 and 536: 504 CHAPTER 27 ■ PROJECT 8: FILE
- Page 537 and 538: 506 CHAPTER 27 ■ PROJECT 8: FILE
- Page 539 and 540: 508 CHAPTER 27 ■ PROJECT 8: FILE
- Page 541 and 542: 510 CHAPTER 27 ■ PROJECT 8: FILE
- Page 543 and 544: 512 CHAPTER 27 ■ PROJECT 8: FILE
- Page 545 and 546: 514 CHAPTER 27 ■ PROJECT 8: FILE
- Page 547 and 548: 516 CHAPTER 27 ■ PROJECT 8: FILE
- Page 549 and 550: 518 CHAPTER 28 ■ PROJECT 9: FILE
- Page 551 and 552: 520 CHAPTER 28 ■ PROJECT 9: FILE
- Page 553 and 554: 522 CHAPTER 28 ■ PROJECT 9: FILE
- Page 555 and 556: 524 CHAPTER 28 ■ PROJECT 9: FILE
- Page 558 and 559: CHAPTER 29 ■ ■ ■ Project 10:
- Page 560 and 561: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 562 and 563: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
484 CHAPTER 26 ■ PROJECT 7: YOUR OWN BULLETIN BOARD<br />
• Posting new messages<br />
• Replying <strong>to</strong> existing messages<br />
In addition <strong>to</strong> these functional requirements, it would be nice if the system was reasonably<br />
stable, could handle a large number of messages, and avoided such problems as two users<br />
writing <strong>to</strong> the same file at the same time. The desired robustness can be achieved by using a<br />
database server of some sort, instead of writing the file-handling code yourself.<br />
Useful Tools<br />
In addition <strong>to</strong> the CGI stuff from Chapter 15, you’ll need an SQL database, as discussed in<br />
Chapter 13. You could either use the stand-alone database SQLite, which is used in that chapter,<br />
or you could use some other system, such as either of the following two excellent, freely<br />
available databases:<br />
• PostgreSQL (http://www.postgresql.org)<br />
• MySQL (http://www.mysql.org)<br />
In this chapter, I use PostgreSQL for the examples, but the code should work with most<br />
SQL databases (including MySQL) with few edits.<br />
Before moving on, you should make sure that you have access <strong>to</strong> an SQL database server<br />
and check its documentation for instructions on how <strong>to</strong> manage it.<br />
In addition <strong>to</strong> the database server itself, you’ll need a <strong>Python</strong> module that can interface<br />
with the server (and hide the details from you). Most such modules support the <strong>Python</strong> DB-API,<br />
which is discussed in more detail in Chapter 13. In this chapter, I use psycopg (http://initd.org/<br />
Software/psycopg), a robust front-end for PostgreSQL. If you’re using MySQL, the MySQLdb<br />
module (http://sourceforge.net/projects/mysql-python) is a good choice.<br />
After you have installed your database module, you should be able <strong>to</strong> import it without<br />
raising any exceptions. For psycopg:<br />
>>> import psycopg<br />
>>><br />
For MySQLdb:<br />
>>> import MySQLdb<br />
>>><br />
You get the idea.<br />
Preparations<br />
Before your program can start using your database, you have <strong>to</strong> actually create the database.<br />
That is done using SQL (see Chapter 13 for some pointers on this).<br />
The database structure is intimately linked with the problem and can be a bit tricky <strong>to</strong><br />
change once you’ve created it and populated it with data (messages). Let’s keep it simple:<br />
You’ll have only one table, which will contain one row for each message. Each message will<br />
have a unique ID (an integer), a subject, a sender (or poster), and some text (the body).