04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 24<br />

■ ■ ■<br />

Project 5: A Virtual Tea Party<br />

In this project, you do some serious network programming. In Chapter 14, I showed you how,<br />

in general terms; now you get to try it out. You’ll write a chat server—a program that lets several<br />

people connect via the Internet and chat with each other in real time. There are many ways to<br />

create such a beast in Python. A simple and natural approach might be to use the Twisted<br />

framework (discussed in Chapter 14), for example, with the LineReceiver class taking center<br />

stage. In this chapter, I stick to the standard libraries, basing the program on the modules<br />

asyncore and asynchat. If you’d like, you could try out some of the alternative methods (such as<br />

forking or threading) discussed in Chapter 14.<br />

What’s the Problem?<br />

Online chatting is becoming quite commonplace. There are many chat services of various<br />

kinds (IRC, instant messengers, and so forth) available all over the Internet; some of these are<br />

even full-fledged text-based virtual worlds (see http://www.mudconnect.com for a long list). If<br />

you want to set up a chat server, there are many free server programs you can download and<br />

install—but writing one yourself is useful for two reasons:<br />

• You learn about network programming.<br />

• You can customize it as much as you want.<br />

The second point suggests that you can start with a simple chat server and develop it into<br />

basically any kind of server (including a virtual world), with all the power of Python at your<br />

fingertips. Pretty awesome, isn’t it?<br />

Specific Goals<br />

Specifically, the program you create must be able to do the following:<br />

• Receive multiple connections from different users.<br />

• Let the users act in parallel.<br />

• Interpret commands such as say or logout.<br />

455

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!