Python Cookbook 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.

你的站点列表里的一些ftp站点应该虽能能够下载.你想过滤这个列表来获取当前开放的站点.

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.
作为另外一个可选的方案,filter(isFTPSiteUp, sites)将提供和列表解析一样的结果列表.

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 sites we 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.
由于站点可能会关闭或者因为种种原因而暂时不开放,ftp站点列表有时候会难以控制.如果我们要给出一个小型的交互程序来让用户在ftp站点中选择,但我们不希望给出未开放的站点,那么这个配方中的代码是简单且能够适合的.如果你每天定义运行这个代码几次并且添加结果到一个文件中去.这个结果将会成为一个长期维护的ftp列表的基础.任何一个站点如果超出一定的时候没有开发都将被移除出主列表并放进一个故障列表中.
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.
一个可以用来过滤非ftp站点列表的非常相似的想法是这样的,使用其他的模块,如NNTP协议的nntplib模块或者HTTP的httplib模块等来代替本配方中的标准Python库模块 ftplib.
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.
当你通过一个程序来检查任何ftp站点时,使用多线程来同时(这样等待不同的站点的响应时间可以重叠),或异步检查多个站点将会更快.这个配方中给出的简单方法非常容易编写和裂解,但是对大多数真实网络程序来说,你要使用多线程或者异步方法来提高贤能,就像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.

标准库socket, ftplib, nntplib, 和 httplib, 以及内置函数 filter,的文档可以再库引用或者Python in a Nutshell中找到.
原文地址:https://www.cnblogs.com/triStoneL/p/1579141.html