python ftp demo

import ftplib
import os
import socket

HOST = 'ftp.mozilla.org'
DIRN = 'pub/mozilla.org/webtools'
FILE = 'bugzilla-LATEST.tar.gz'

#username = 'socmax'
#pwd = '#2009@socmaxcom'

def main():
    try:
        f = ftplib.FTP(HOST)
    except (socket.error,socket.gaierror), e:
        print 'ERROR: cannot reach "%s"' % HOST
        return
    print '*** connected to host "%s"' % HOST

    try:
#        f.login(username,pwd)
         f.login()
    except ftplib.error_perm:
        print 'ERROR: cannot login'
        f.quit()
        return
#    print '*** login the ftp by user "%s" ***' % username
    print '*** login the ftp by user'
    try:
        f.cwd(DIRN)
    except ftplib.error_perm:
        print 'Error: cannot cd to "%s"' % DIRN
        f.quit()
        return
    print '*** changed to "%s" folder' % DIRN

    try:
        f.retrbinary('RETR %s' % FILE,open(FILE, 'wb').write)
    except ftplib.error_perm:
        print 'Error,cannot read file "%s"' % FILE
        os.unlink(FILE)
    else:
        print '*** Download "%s" ok' % FILE
    f.quit()
    return
if __name__ == '__main__':
    main()
 python ftp demo

你可能感兴趣的:(python,socket,F#,OS)