|
# Coding: UTF-8
From ctypes import *
Import OS
Import sys
Import ftplib
Class myFtp:
Ftp = ftplib. FTP ()
BIsDir = False
Path = ""
Def _ init _ (self, host, port = '21 '):
Self. ftp. set_debuglevel (2) # enable debug level 2 and display details
# Self. ftp. set_pasv (0) #0 Active Mode 1 # Passive Mode
Self. ftp. connect (host, port)
Def Login (self, user, passwd ):
Self. ftp. login (user, passwd)
Print self. ftp. welcome
Def DownLoadFile (self, LocalFile, RemoteFile ):
File_handler = open (LocalFile, 'wb ')
Self. ftp. retrbinary ("RETR % s" % (RemoteFile), file_handler.write)
File_handler.close ()
Return True
Def UpLoadFile (self, LocalFile, RemoteFile ):
If OS. path. isfile (LocalFile) = False:
Return False
File_handler = open (LocalFile, "rb ")
Self. ftp. storbinary ('stor % s' % RemoteFile, file_handler, 4096)
File_handler.close ()
Return True
Def UpLoadFileTree (self, LocalDir, RemoteDir ):
If OS. path. isdir (LocalDir) = False:
Return False
LocalNames = OS. listdir (LocalDir)
Print RemoteDir
Self. ftp. cwd (RemoteDir)
For Local in LocalNames:
Src = OS. path. join (LocalDir, Local)
If OS. path. isdir (src ):
Self. UpLoadFileTree (src, Local)
Else:
Self. UpLoadFile (src, Local)
Self. ftp. cwd ("..")
Return
Def DownLoadFileTree (self, LocalDir, RemoteDir ):
If OS. path. isdir (LocalDir) = False:
OS. makedirs (LocalDir)
Self. ftp. cwd (RemoteDir)
RemoteNames = self. ftp. nlst ()
For file in RemoteNames:
Local = OS. path. join (LocalDir, file)
If self. isDir (file ):
Self. DownLoadFileTree (Local, file)
Else:
Self. DownLoadFile (Local, file)
Self. ftp. cwd ("..")
Return
Def show (self, list ):
Result = list. lower (). split ("")
If self. path in result and "<dir>" in result:
Self. bIsDir = True
Def isDir (self, path ):
Self. bIsDir = False
Self. path = path
# This ues callback function, that will change bIsDir value
Self. ftp. retrlines ('LIST', self. show)
Return self. bIsDir
Def close (self ):
Self. ftp. quit ()
Ftp = myFtp ('********')
Ftp. Login ('*****','*****')
# Ftp. DownLoadFile ('test. TXT ', 'oss \ runtime. log') # OK
# Ftp. UpLoadFile ('runtime. log', 'oss \ runtime. log') # OK
# Ftp. DownLoadFileTree ('bcd', 'oss \ abc') # OK
# Ftp. UpLoadFileTree ('aaa', "others \\")
Ftp. close ()
Print "OK! "
|