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 471 def enter(self, room): # Remove self from current room and add self to # next room... try: cur = self.room except AttributeError: pass else: cur.remove(self) self.room = room room.add(self) def collect_incoming_data(self, data): self.data.append(data) def found_terminator(self): line = ''.join(self.data) self.data = [] try: self.room.handle(self, line) except EndSession: self.handle_close() def handle_close(self): async_chat.handle_close(self) self.enter(LogoutRoom(self.server)) class ChatServer(dispatcher): """ A chat server with a single room. """ def __init__(self, port, name): dispatcher.__init__(self) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.set_reuse_addr() self.bind(('', port)) self.listen(5) self.name = name self.users = {} self.main_room = ChatRoom(self) def handle_accept(self): conn, addr = self.accept() ChatSession(self, conn) if __name__ == '__main__': s = ChatServer(PORT, NAME) try: asyncore.loop() except KeyboardInterrupt: print
472 CHAPTER 24 ■ PROJECT 5: A VIRTUAL TEA PARTY Table 24-1. The Commands Available in the Chat Server Command Available In . . . Description login name Login room Used to log into the server logout All rooms Used to log out of the server say statement Chat room(s) Used to say something look Chat room(s) Used to find out who is in the same room who Chat room(s) Used to find out who is logged on to the server Further Exploration You can do a lot to extend and enhance the basic server presented in this chapter: • You could make a version with multiple chat rooms, and you could extend the command set to make it behave in any way you want. • You might want to make the program recognize only certain commands (such as login or logout) and treat all other text entered as general chatting, thereby avoiding the need for a say command. • You could prefix all commands with a special character (for example, a slash, giving commands like /login and /logout) and treat everything that doesn’t start with the specified character as general chatting. • You might want to create your own GUI client—but that’s a bit trickier than it might seem. The server has one event loop, and the GUI toolkit will have another. To make them cooperate, you will need to use threading. (For an example of how this can be done in simple cases where the various threads don’t directly access each other’s data, see Chapter 28.) What Now? Now you’ve got your very own chat server. In the next project you tackle a different type of network programming: CGI, the mechanism underlying most Web applications (as discussed in Chapter 15). The specific application of this technology in the next project is remote editing, which enables several users to collaborate on developing the same document. You may even use it to edit your own Web pages remotely.
- 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 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 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
- 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
472 CHAPTER 24 ■ PROJECT 5: A VIRTUAL TEA PARTY<br />
Table 24-1. The Commands Available in the Chat Server<br />
Command Available In . . . Description<br />
login name Login room Used <strong>to</strong> log in<strong>to</strong> the server<br />
logout All rooms Used <strong>to</strong> log out of the server<br />
say statement Chat room(s) Used <strong>to</strong> say something<br />
look Chat room(s) Used <strong>to</strong> find out who is in the same room<br />
who Chat room(s) Used <strong>to</strong> find out who is logged on <strong>to</strong> the server<br />
Further Exploration<br />
You can do a lot <strong>to</strong> extend and enhance the basic server presented in this chapter:<br />
• You could make a version with multiple chat rooms, and you could extend the command<br />
set <strong>to</strong> make it behave in any way you want.<br />
• You might want <strong>to</strong> make the program recognize only certain commands (such as login<br />
or logout) and treat all other text entered as general chatting, thereby avoiding the need<br />
for a say command.<br />
• You could prefix all commands with a special character (for example, a slash, giving<br />
commands like /login and /logout) and treat everything that doesn’t start with the<br />
specified character as general chatting.<br />
• You might want <strong>to</strong> create your own GUI client—but that’s a bit trickier than it might<br />
seem. The server has one event loop, and the GUI <strong>to</strong>olkit will have another. To make<br />
them cooperate, you will need <strong>to</strong> use threading. (For an example of how this can be done<br />
in simple cases where the various threads don’t directly access each other’s data, see<br />
Chapter 28.)<br />
What Now?<br />
Now you’ve got your very own chat server. In the next project you tackle a different type of<br />
network programming: CGI, the mechanism underlying most Web applications (as discussed<br />
in Chapter 15). The specific application of this technology in the next project is remote editing,<br />
which enables several users <strong>to</strong> collaborate on developing the same document. You may even<br />
use it <strong>to</strong> edit your own Web pages remotely.