Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 24 ■ ■ ■ Project 5: A Virtual Tea Party In this project, you do some serious network programming. In Chapter 14, I showed you how, in general terms; now you get to try it out. You’ll write a chat server—a program that lets several people connect via the Internet and chat with each other in real time. There are many ways to create such a beast in Python. A simple and natural approach might be to use the Twisted framework (discussed in Chapter 14), for example, with the LineReceiver class taking center stage. In this chapter, I stick to the standard libraries, basing the program on the modules asyncore and asynchat. If you’d like, you could try out some of the alternative methods (such as forking or threading) discussed in Chapter 14. What’s the Problem? Online chatting is becoming quite commonplace. There are many chat services of various kinds (IRC, instant messengers, and so forth) available all over the Internet; some of these are even full-fledged text-based virtual worlds (see http://www.mudconnect.com for a long list). If you want to set up a chat server, there are many free server programs you can download and install—but writing one yourself is useful for two reasons: • You learn about network programming. • You can customize it as much as you want. The second point suggests that you can start with a simple chat server and develop it into basically any kind of server (including a virtual world), with all the power of Python at your fingertips. Pretty awesome, isn’t it? Specific Goals Specifically, the program you create must be able to do the following: • Receive multiple connections from different users. • Let the users act in parallel. • Interpret commands such as say or logout. 455
456 CHAPTER 24 ■ PROJECT 5: A VIRTUAL TEA PARTY • Enable each individual session to behave according to the state the user is in (connected, logged in, logging out, and so on). • Be easily extensible. The two things that will require special tools are the network connections and the parallel nature of the program. Useful Tools The only new tools you need in this project are the asyncore module from the standard library and its relative asynchat. I’ll describe the basics of how these work; you can find more details about them in the Python Library Reference (http://python.org/doc/lib/module-asyncore.html and http://python.org/doc/lib/module-asynchat.html). What’s It For? As discussed in Chapter 14, the basic component in a network program is the socket. Sockets can be created directly by importing the socket module and using the functions there. So what do you need asyncore for? The asyncore framework enables you to juggle several users who are connected simultaneously. Imagine a scenario in which you have no special tools for handling this. When you start up the server, it awaits connecting users. When one user is connected, it starts reading data from that user and supplying results through a socket. But what happens if another user is already connected? The second user to connect must wait until the first one has finished. In some cases that will work just fine, but when you’re writing a chat server, the whole point is that more than one user can be connected—how else could users chat with one another? The asyncore framework is based on an underlying mechanism (the select function from the select module, as discussed in Chapter 14) that allows the server to serve all the connected users in a piecemeal fashion. Instead of reading all the available data from one user before going on to the next, only some data is read. Also, the server reads only from the sockets where there is data to be read. This is done again and again, in a loop. Writing is handled similarly. You could implement this yourself using just the modules socket and select, but asyncore and asynchat provide a very useful framework that takes care of the details for you. ■Tip For alternative ways of implementing parallel user connections, see the section “Multiple Connections” in Chapter 14. Preparations The first thing you need is a computer that’s connected to a network (such as the Internet)— otherwise others won’t be able to connect to your chat server. (It is possible to connect to the
- Page 435 and 436: 404 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 437 and 438: 406 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 439 and 440: 408 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 442 and 443: CHAPTER 21 ■ ■ ■ Project 2: P
- Page 444 and 445: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 446 and 447: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 448 and 449: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 450 and 451: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 452 and 453: CHAPTER 22 ■ ■ ■ Project 3: X
- Page 454 and 455: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 456 and 457: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 458 and 459: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 460 and 461: CHAPTER 22 ■ PROJECT 3: XML FOR A
- 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 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 515 and 516: 484 CHAPTER 26 ■ PROJECT 7: YOUR
- 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
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 <strong>to</strong> 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 <strong>to</strong><br />
create such a beast in <strong>Python</strong>. A simple and natural approach might be <strong>to</strong> use the Twisted<br />
framework (discussed in Chapter 14), for example, with the LineReceiver class taking center<br />
stage. In this chapter, I stick <strong>to</strong> 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 <strong>to</strong> 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 cus<strong>to</strong>mize it as much as you want.<br />
The second point suggests that you can start with a simple chat server and develop it in<strong>to</strong><br />
basically any kind of server (including a virtual world), with all the power of <strong>Python</strong> at your<br />
fingertips. Pretty awesome, isn’t it?<br />
Specific Goals<br />
Specifically, the program you create must be able <strong>to</strong> 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