Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 23 ■ ■ ■ Project 4: In the News In this project, you see how you go from a simple prototype without any form of abstraction (no functions, no classes) to a generic system in which some important abstractions have been added. Also, you get a brief introduction to the nntplib library. WHAT IS NNTP? Network News Transfer Protocol (NNTP) is a standard network protocol for managing messages posted on Usenet discussion groups. NNTP servers form a global network that collectively manage these newsgroups, and through an NNTP client (also called a newsreader) you can post and read messages. Most recent Web browsers include NNTP clients, and separate clients exist as well. For more information about Usenet, you may check out the informational Web site at http:// www.usenet.org. What’s the Problem? The program you write in this project will be an information-gathering agent, a program that will be able to gather information (more specifically, news) and compile a report for you. Given the network functionality you have already encountered, that might not seem very difficult— and it isn’t, really. But in this project you go a bit beyond the simple “download a file with urllib” approach. You use another network library that is a bit more difficult to use than urllib, namely nntplib. In addition, you get to refactor the program to allow many types of news sources and various types of destinations, making a clear separation between the front-end and the backend, with the main engine in the middle. ■Note Refactoring means improving the design of an existing program, usually by adding abstraction and structure. 439
440 CHAPTER 23 ■ PROJECT 4: IN THE NEWS Specific Goals The main goals for the final program are as follows: • The program should be able to gather news from many different sources. • It should be easy to add new news sources (and even new kinds of sources). • The program should be able to dispatch its compiled news report to many different destinations, in many different formats. • It should be easy to add new destinations (and even new kinds of destinations). Useful Tools In this project, there is no need to install separate software. What you need, however, are some standard library modules, including one that you haven’t seen before, nntplib, which deals with NNTP servers. Instead of explaining all the details of that module, let’s examine it through some prototyping. You will also be using the time module. See Chapter 10 for more information. Preparations To be able to use nntplib you need to have access to an NNTP server. If you’re not sure whether you do, you could ask your ISP or system administrator for details. In the code examples in this chapter, I use the newsgroup comp.lang.python.announce, so you should make sure that your news (NNTP) server has that group, or you should find some other group you’d like to use. It is important that the NNTP server support the NEWNEWS command—if it doesn’t, the programs in this chapter won’t work. (If you don’t know whether your server supports this command or not, simply try to execute the programs and see what happens.) If you don’t have access to an NNTP server, or your server’s NEWNEWS command is disabled, several open servers are available for anyone to use. A quick Web search for “free nntp server” ought to give you a number of servers to choose from. The Web site http://newzbot.com also contains many useful resources. Assuming that your news server is news.foo.bar (this is not a real server name, and won’t work), you can test your NNTP server like this: >>> from nntplib import NNTP >>> server = NNTP('news.foo.bar') >>> server.group('comp.lang.python.announce')[0] ■Note To connect to some servers, you may need to supply additional parameters for authentication. Please consult the Python Library Reference for details on the optional parameters of the NNTP constructor.
- Page 418 and 419: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 420: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 423 and 424: 392 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 425 and 426: 394 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 427 and 428: 396 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 429 and 430: 398 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 431 and 432: 400 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 433 and 434: 402 CHAPTER 20 ■ PROJECT 1: INSTA
- 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 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 515 and 516: 484 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 517 and 518: 486 CHAPTER 26 ■ PROJECT 7: YOUR
440 CHAPTER 23 ■ PROJECT 4: IN THE NEWS<br />
Specific Goals<br />
The main goals for the final program are as follows:<br />
• The program should be able <strong>to</strong> gather news from many different sources.<br />
• It should be easy <strong>to</strong> add new news sources (and even new kinds of sources).<br />
• The program should be able <strong>to</strong> dispatch its compiled news report <strong>to</strong> many different<br />
destinations, in many different formats.<br />
• It should be easy <strong>to</strong> add new destinations (and even new kinds of destinations).<br />
Useful Tools<br />
In this project, there is no need <strong>to</strong> install separate software. What you need, however, are some<br />
standard library modules, including one that you haven’t seen before, nntplib, which deals<br />
with NNTP servers. Instead of explaining all the details of that module, let’s examine it through<br />
some pro<strong>to</strong>typing.<br />
You will also be using the time module. See Chapter 10 for more information.<br />
Preparations<br />
To be able <strong>to</strong> use nntplib you need <strong>to</strong> have access <strong>to</strong> an NNTP server. If you’re not sure whether<br />
you do, you could ask your ISP or system administra<strong>to</strong>r for details. In the code examples in this<br />
chapter, I use the newsgroup comp.lang.python.announce, so you should make sure that your<br />
news (NNTP) server has that group, or you should find some other group you’d like <strong>to</strong> use. It is<br />
important that the NNTP server support the NEWNEWS command—if it doesn’t, the programs in<br />
this chapter won’t work. (If you don’t know whether your server supports this command or not,<br />
simply try <strong>to</strong> execute the programs and see what happens.)<br />
If you don’t have access <strong>to</strong> an NNTP server, or your server’s NEWNEWS command is disabled,<br />
several open servers are available for anyone <strong>to</strong> use. A quick Web search for “free nntp server”<br />
ought <strong>to</strong> give you a number of servers <strong>to</strong> choose from. The Web site http://newzbot.com also<br />
contains many useful resources.<br />
Assuming that your news server is news.foo.bar (this is not a real server name, and won’t<br />
work), you can test your NNTP server like this:<br />
>>> from nntplib import NNTP<br />
>>> server = NNTP('news.foo.bar')<br />
>>> server.group('comp.lang.python.announce')[0]<br />
■Note To connect <strong>to</strong> some servers, you may need <strong>to</strong> supply additional parameters for authentication.<br />
Please consult the <strong>Python</strong> Library Reference for details on the optional parameters of the NNTP construc<strong>to</strong>r.