设为首页 收藏本站
查看: 1146|回复: 0

Python Cookbook 13.3. Filtering a List of FTP Sites

[复制链接]

尚未签到

发表于 2015-5-29 09:02:20 | 显示全部楼层 |阅读模式
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 TrueNow, a simple list comprehension can perform the recipe's task, but we may as  well wrap that list comprehension inside another function:
现在,一个简单的列表解析可以完成这个配方中的任务,但是我们也可以讲这个列表解析包装在一个函数里:

DSC0000.gif 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中找到.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-71736-1-1.html 上篇帖子: 一个FTP客户端的C#代码 下篇帖子: Firefox 3.0访问FTP乱码问题解决
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表