Welcome, guest | Sign In | My Account | Store | Cart

A pair of functions for checking whether FTP sites are up. The refineFTPList() function will take in a list of FTP sites and returns a list of sites that are not down. The isFTPSiteUp() function checks a particular FTP site to see if it is up.

Python, 26 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import socket
from ftplib import FTP
true, false = 1, 0

def isFTPSiteUp(site):
	try:
	        FTP(site).quit()
		
	except(socket.error):
		return false
	return true


def refineFTPList(list):
	new_list = []

	for site in list:
		if isFTPSiteUp(site):
			new_list.append(site)
	
	return new_list	
				
sites = ['ftp.cdrom.com', 'ftp.redhat.com', 'ftp.ska143blah.com']

working_sites = refineFTPList(sites)
print working_sites

FTP lists are sometimes difficult to maintain. Hence arises the need to be able to verify that a site is still up and running. This code could easily be applied to add a measure of self-management to an FTP list.

2 comments

Guy Argo 18 years, 10 months ago  # | flag

simpler definition of refineFTPList. def refineFTPList(sites): return filter( isFTPSiteUp, sites )

Guy Argo 18 years, 10 months ago  # | flag

another refactoring. False is a built-in.

Not sure that we care about the specific exception raised.

quit returns a non-null value (usually '221 Goodbye.') when it succeeds.

So how about:

from ftplib import FTP

def isFTPSiteUp(site):
    try:
        return FTP(site).quit()
    except:
        return False

def refineFTPList(sites): return filter( isFTPSiteUp, sites )

print refineFTPList( ['ftp.cdrom.com', 'ftp.redhat.com', 'ftp.ska143blah.com'] )