Section 17.6. Exercises


17.6. Exercises

FTP

17-1.

Simple FTP Client. Given the FTP examples from this chapter, write a small FTP client program that goes to your favorite Web sites and downloads the latest versions of the applications you use. This may be a script that you run every few months to make sure you're using the "latest and greatest." You should probably keep some sort of table with FTP location, login, and password for your convenience.

17-2.

Simple FTP Client and Pattern-Matching. Use your solution to the previous exercise as a starting point for creating another simple FTP client that either pushes or pulls a set of files from a remote host using patterns. For example, if you want to move a set of Python or PDF files from one host to another, allow users to enter "*.py" or "doc*.pdf" and only transfer those files whose names match.

17-3.

Smart FTP Command-Line Client. Create a command-line FTP application similar to the vanilla Unix /bin/ftp program, however, make it a "better FTP client," meaning it should have additional useful features. You can take a look at the ncFTP application as motivation. It can be found at http://ncftp.com. For example, it has the following features: history, bookmarks (saving FTP locations with log in and password), download progress, etc. You may have to implement readline functionality for history and curses for screen control.

17-4.

FTP and Multithreading. Create an FTP client that uses Python threads to download files. You can either upgrade your existing Smart FTP client as in the previous problem, or just write a more simple client to download files. This can be either a command-line program where you enter multiple files as arguments to the program, or a GUI where you let the user select 1+ file(s) to transfer. Extra credit: Allow patterns, i.e., *.exe. Use individual threads to download each file.

17-5.

FTP and GUI. Take your smart FTP client developed above and add a GUI layer on top of it to form a complete FTP application. You may choose from any of the modern Python GUI toolkits.

17-6.

Subclassing. Derive ftplib.FTP and make a new class FTP2 where you do not need to give "STOR filename" and "RETR filename" commands with all four (4) retr*() and stor*() methods ... you only need to pass in the filename. You may choose to either override the existing methods or create new ones with a '2' suffix, i.e., retrlines2().

The file Tools/scripts/ftpmirror.py in the Python source distribution is a script that can mirror FTP sites, or portions thereof, using the ftplib module. It can be used as an extended example that applies to this module. The next five problems feature creating solutions that revolve around code like ftpmirror.py. You may use code in ftpmirror.py or implement your own solution with its code as your motivation.

17-7.

Recursion. The ftpmirror.py script copies a remote directory recursively. Create a simpler FTP client in the spirit of ftpmirror.py but one that does not recurse by default. Create an "-r" option that tells the application to recursively copy subdirectories to the local filesystem.

17-8.

Pattern-Matching. The ftpmirror.py script has an "-s" option that lets users skip files that match the given pattern, i.e., ".exe." Create your own simpler FTP client or update your solution to the previous exercise so that it lets the user supply a pattern and only copy those files matching that pattern. Use your solution to an earlier problem above as a starting point.

17-9.

Recursion and Pattern-Matching. Create an FTP client that integrates both of the previous exercises.

17-10.

Recursion and ZIP files. This problem is similar to the first recursion exercise aboveinstead of copying the remote files to the local filesystem, either update your existing FTP client or create a new one to download remote files and compress them into a ZIP (or TGZ or BZ2) file. This "-z" option allows your users to back up an FTP site in an automated manner.

17-11.

Kitchen Sink. Implement a single, final, all-encompassing FTP application that has all the solutions to the exercises above, i.e., "-r", "-s", and "-z" options.

NNTP

17-12.

Introduction to NNTP. Change Example 17.2 (getLatestNNTP.py) so that instead of the most recent article, it displays the first available article meaningfully.

17-13.

Improving Code. Fix the flaw in getLatestNNTP.py where triple-quoted lines show up in the output. This is because we want to display Python interactive interpreter lines but not triple-quoted text. Solve this problem by checking whether the stuff that comes after the ">>>" is real Python code. If so, display it as a line of data; if not, do not display this quoted text. Extra credit: Use your solution to solve another minor problem: leading whitespace is not stripped from the body because it may represent indented Python code. If it really is code, display it; otherwise, it is text so lstrip() that before displaying.

17-14.

Finding Articles. Create an NNTP client application that lets the user log in and choose a newsgroup of interest. Once that has been accomplished, prompt the user for keywords to search article Subject lines for. Bring up the list of articles that match the requirement and display them to the user. The user should then be allowed to choose an article to read from that listdisplay them and provide simple navigation like pagination, etc. If no search field is entered, bring up all current articles.

17-15.

Searching Bodies. Upgrade your solution to the previous problem by searching both Subject lines and article bodies. Allow for AND or OR searching of keywords. Also allow for AND or OR searching of Subject lines and article bodies, i.e., keyword(s) must be in Subject lines only, article bodies only, either, or both.

17-16.

Threaded Newsreader. This doesn't mean write a multithreaded newsreaderit means organize related postings into "article threads." In other words, group related articles together, independent of when the individual articles were posted. All the articles belonging to individual threads should be listed chronologically though. Allow the user to:

  1. select individual articles (bodies) to view, then have the option to go back to the list view or to previous or next article either sequentially or related to the current thread.

  2. allow replies to threads, option to copy and quote previous article, reply to the entire newsgroup via another post. Extra credit: Allow personal reply to individual via e-mail.

  3. permanently delete threadsno future related articles should show up in the article list. For this, you will have to temporarily keep a persistent list of deleted threads so that they don't show up again. You can assume a thread is dead if no one posts an article with the same Subject line after several months.

17-17.

GUI Newsreader. Similar to an FTP exercise above, choose a Python GUI toolkit to implement a complete standalone GUI newsreader application.

17-18.

Refactoring. Like ftpmirror.py for FTP, there is a demo script for NNTP: Demo/scripts/newslist.py. Run it. This script was written a long time ago and can use a facelift. For this exercise, you are to refactor this program using features of the latest versions of Python as well as your developing skills in Python to perform the same task but run and complete in less time. This can include using list comprehensions or generator expressions, using smarter string concatenation, not calling unnecessary functions, etc.

17-19.

Caching. Another problem with newslist.py is that, according to its author, "I should really keep a list of ignored empty groups and re-check them for articles on every run, but I haven't got around to it yet." Make this improvement a reality. You may use the default version as-is or your newly improved one from the previous exercise.

E-MAIL

17-20.

Identifiers. The POP3 method pass_() is used to send the password to the server after giving it the login name using login(). Can you give any reasons why you believe this method was named with a trailing underscore, i.e., "pass_()", instead of just plain old "pass()"?

17-21.

IMAP. Now that you are familiar with how POP works, your experience will help you with an IMAP client. Study the IMAP protocol RFC document, and use the Python imaplib module to help you.

The next set of exercises deal with the myMail.py application found in this chapter (Example 17.3).

17-22.

E-mail Headers. In myMail.py, the last few lines compared the originally sent body with the body in the received e-mail. Create similar code to assert the original headers. Hint: Ignore newly added headers.

17-23.

Error Checking. Add SMTP and POP3 error-checking.

17-24.

SMTP and IMAP. Take our simple myMail.py, and added support for IMAP. Extra credit: Support both mail download protocols, letting the user choose which to use.

17-25.

E-mail Composition. Further develop your solution to the previous problem by giving the users of your application the ability to compose and send e-mail.

17-26.

E-mail Application. Further develop your e-mail application, turning it into something more useful by adding in mailbox management. Your application should be able to read in the current set of e-mail messages in a user's imbeds and display their Subject lines. Users should be able to select messages to view. Extra credit: Add support to view attachments via external applications.

17-27.

GUI. Add a GUI layer on top of your solution to the previous problem to make it practically a full e-mail application.

17-28.

Elements of SPAM. Unsolicited junk e-mail, or spam, is a very real and significant problem today. There are many good solutions out there, validating this market. We do not want you to (necessarily) reinvent the wheel but we would like you to get a taste of some of the elements of spam.

  1. "mbox" format. Before we can get started, we should convert any e-mail messages you want to work on to a common format, such as the "mbox" format. (There are others that you can use if you prefer. Once you have several (or all) work messages in mbox format, merge them all into a single file.

  2. Headers. Most of the clues of spam lie in the e-mail headers. (You may wish to use the email package or parse them manually yourself.) Write code that answers questions such as:

    - What e-mail client appears to have originated this message? (Check out the X-Mailer header.)

    - Is the message ID (Message-ID header) format valid?

    - Are there domain name mismatches between the From, Received, and perhaps Return-Path headers? What about domain name and IP address mismatches? Is there an X-Authentication-Warning header? If so, what does it report?

  3. Information Servers. Based on an IP address or domain, servers such as WHOIS, SenderBase.org, etc., may be able to help you identify the location where a piece of bulk e-mail originated. Find one or more of these services and build code to the find the country of origin, and optionally the city, network owner name, contact info, etc.

  4. Keywords. Certain words keep popping up in spam. You have no doubt seen them before, and in all of their variations, including using a number resembling a letter, capitalizing random letters, etc. Build a list of frequent words that you have seen definitely tied to spam, and quarantine such messages as possible spam. Extra credit: Develop an algorithm or add keyword variations to spot such trickery in messages.

  5. Phishing. These spam messages attempt to disguise themselves as valid e-mail from major banking institutions or well-known Internet Web sites. They contain links that lure readers to Web sites in an attempt to harvest private and extremely sensitive information such as login names, passwords, and credit card numbers. These fakers do a pretty good job of giving their fraudulent messages an accurate look-and-feel. However, they cannot hide the fact that the actual link that they direct users to does not belong to the company they are masquerading as. Many of them are obvious giveaways, i.e., horrible-looking domain names, raw IP addresses, and even IP addresses in 32-bit integer format rather than in octets. Develop code that can determine whether e-mail that looks like official communication is real or bogus.

Miscellaneous

A list of various Internet protocols, including the three highlighted in this chapter, can be found at http://www.networksorcery.com/enp/topic/ipsuite.htm# Application%20layer%20protocols. A list of specific Internet protocols supported by Python (currently), can be found at http://docs.python.org/lib/internet.html

17-29.

Developing Alternate Internet Clients. Now that you have seen four examples of how Python can help you develop Internet clients, choose another protocol with client support in a Python standard library module and write a client application for it.

17-30.

*Developing New Internet Clients. Much more difficult: find an uncommon or upcoming protocol without Python support and implement it. Be serious enough that you will consider writing and submitting a PEP to have your module included in the standard library distribution of a future Python release.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net