Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] - نسخه متنی

David Ascher, Alex Martelli, Anna Ravenscroft

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Recipe 13.3. Filtering a List of FTP Sites


Credit: Mark Nenadov


Problem



Several of the FTP sites on your list of
sites could be down at any time. You want to filter that list and
obtain the list of those sites that are currently up.


Solution


Clearly, we first need a function to check whether one particular
site is up:

import socket, ftplib
def isFTPSiteUp(site):
try:
ftplib.FTP(site).quit( )
except socket.error:
return False
else:
return True

Now, a simple list comprehension can perform the
recipe's task, but we may as well wrap that list
comprehension inside another function:

def filterFTPsites(sites):
return [site for site in sites if isFTPSiteUp(site)]

Alternatively, filter(isFTPSiteUp, sites) returns
exactly the same resulting list as the list comprehension.


Discussion


Lists of FTP sites are sometimes difficult to maintain, since sites
may be closed or temporarily down for all sorts of reasons. The code
in this recipe is simple and suitable, for example, for use inside a
small interactive program that must let the user choose among FTP
siteswe may as well not even present for choice those sites we
know are down! If you run this code regularly a few times a day and
append the results to a file, the results may also be a basis for
long-term maintenance of a list of FTP sites. Any site that has been
down for more than a certain number of days should probably be moved
away from the main list and into a list of sites that may well have
croaked.

Very similar ideas could be used to filter lists of sites that serve
protocols other than FTP, by using, instead of standard Python
library module ftplib, other such modules, such as
nntplib for the NNTP protocol,
httplib for the Hypertext Transport Protocol
(HTTP), and so on.

When you're checking many FTP sites within one
program run, it could be much faster to use multiple threads to check
on multiple sites at once (so that the delays while waiting for the
various sites to respond can overlap), or else use an asynchronous
approach. The simple approach presented in this recipe is easiest to
program and to understand, but for most real-life networking
programs, you do want to enhance performance by using either
multithreading or asynchronous approaches, as other recipes in this
chapter demonstrate.


See Also


Documentation for the standard library modules
socket, ftplib,
nntplib, and httplib, and
built-in function filter, in the
Library Reference and Python in a
Nutshell
.


    / 394