#!/usr/bin/env python #-*- coding: iso8859-15 -*- ########################################################## # FTP v0.2 # ########################################################## # Author: Juan Miguel Taboada Godoy # # Date: Szczecin, 21th of december of 2005 # # Description: FTP connection manager # # Versión: 2005122101 # # # # Codigo fuente bajo licencia GNU/GPL # # Centrologic (Computational Logistic Center) # # http://www.centrologic.com - info@centrologic.com # ########################################################## # Librerías que voy a usar import ftplib ## \internal ## FTP: Libreria para acceder a un servidor FTP ## \version 21/12/2005 1600 Szczecin class FTP: # Constructor {{{1 ## Constructor, start the connection with the server ## \param self - ## \param host Servidor FTP ## \param puerto Puerto FTP ## \param user Usuario de acceso al FTP ## \param password Clave de acceso al FTP ## \exception ConnectionError Cuando falla el intento de conexion def __init__(self,host,puerto,user,password): # Start connection with the FTP server try: self.__stream=ftplib.FTP() self.__stream.connect(host,puerto) self.__stream.login(user,password) self.__connected=1 except Exception,e: self.__connected=0 raise ConnectionError,"Connecting process error: %s" % (e) # }}}1 # Destructor {{{1 ## Destructor, ordena la desconexion del servidor antes de destruir el objeto ## \param self - def __del__(self): self.DISCONNECT() # }}}1 # Method to DISCONNECT (disconnect from server) {{{1 ## Desconecta del servidor FTP ## \param self - ## \exception ConnectionError Si falla el intento de desconexion def DISCONNECT(self): if (self.__connected): # Finish connection with the FTP server try: self.__stream.quit() self.__stream.close() self.__connected=0 except Exception,e: raise ConnectionError,"Disconnecting process error: %s" % e # }}}1 # Method to PUT (send a file) {{{1 ## Coloca el archivo indicado en el servidor remoto ## \param self - ## \param filename Nombre del fichero a enviar al servidor ## \param timeout Tiempo de espera maximo (Dummy: no usar) ## \exception TransferError Si se produce un error durante el envío del fichero ## \exception ConnectionError Si no estamos conectados al servidor def PUT(self,filename,timeout=-1): if (self.__connected): try: # Open the localfile FILE = open(filename, 'r') # Put the file using STOR self.__stream.storbinary('STOR %s' % filename, FILE) # Close the file FILE.close() except Exception,e: raise TransferError,"PUT Error: %s" % e else: raise ConnectionError,"No active connection" # }}}1 # Method to GET (get a file) {{{1 ## Recoje un archivo indicado en el servidor remoto ## \param self - ## \param filename Nombre del fichero que se desea recibir del servidor ## \param timeout Tiempo de espera maximo (Dummy: no usar) ## \exception TransferError Si se produce un error durante el envío del fichero ## \exception ConnectionError Si no estamos conectados al servidor def GET(self,filename,timeout=-1): if (self.__connected): try: # Open the localfile FILE = open(filename, 'wb') # Get the file using RETR self.__stream.retrbinary('RETR %s' % filename, FILE.write) # Close the file FILE.close() except Exception,e: raise TransferError,"GET Error: %s" % e else: raise ConnectionError,"No active connection" # }}}1 # Method to EXISTD (check if a directory exists) {{{1 ## Comprueba si existe un directorio en el servidor remoto ## \param self - ## \param folder Nombre del directorio que deseamos saber si existe o no ## \exception TransferError Si se produce un error durante la comprobacion ## \exception ConnectionError Si no estamos conectados al servidor def EXISTD(self,folder): if (self.__connected): try: # Save the actual folder actual=self.__stream.pwd() # Check that we are not in the asked folder if actual != folder: # We are not in the folder to check try: # I move to the directory self.__stream.cwd(folder) except: # Go back to the "actual" folder self.__stream.cwd(actual) # The folder doesn't exists return 0 # Go back to the "actual" folder self.__stream.cwd(actual) # The folder exists return 1 except Exception,e: raise TransferError,"EXISTD Error: %s" % e else: raise ConnectionError,"No active connection" # }}}1 # Method to EXISTF (check if a file exists) {{{1 ## Comprueba si existe un archivo en el servidor remoto ## \param self - ## \param filename Nombre del fichero que deseamos saber si existe o no ## \exception TransferError Si se produce un error durante la comprobacion ## \exception ConnectionError Si no estamos conectados al servidor def EXISTF(self,filename): if (self.__connected): try: # Check the size self.__stream.size(filename) # The file exists return 1 except: # The file doesn't exits return 0 else: raise ConnectionError,"No active connection" # }}}1 # Method to CD (change the folder) {{{1 ## Cambia de directorio en el servidor remoto ## \param self - ## \param folder Directorio al que deseamos movernos ## \exception TransferError Si se produce un error durante el cambio de directorio ## \exception ConnectionError Si no estamos conectados al servidor def CD(self,folder): if (self.__connected): try: # Change the folder self.__stream.cwd(folder) except Exception,e: raise TransferError,"CD Error: %s" % e else: raise ConnectionError,"No active connection" # }}}1 # Method to REMOVE (delete a file) {{{1 ## Borra el archivo indicado del servidor remoto ## \param self - ## \param filename Nombre del fichero que se desea borrar del servidor ## \exception TransferError Si se produce un error durante el envío del fichero ## \exception ConnectionError Si no estamos conectados al servidor def REMOVE(self,filename): if (self.__connected): try: # Delete the file self.__stream.delete(filename) except Exception,e: raise TransferError,"REMOVE Error: %s" % e else: raise ConnectionError,"No active connection" # }}}1 # Method to PWD (get the actual working folder) {{{1 ## Comprueba en que directorio estamos en el servidor remoto ## \param self - ## \exception TransferError Si se produce un error durante la comprobacion ## \exception ConnectionError Si no estamos conectados al servidor ## \return Devuelve el directorio en el que estamos en el servidor remoto def PWD(self): if (self.__connected): try: pwd=self.__stream.pwd() except Exception,e: raise TransferError,"PWD Error: %s" % e return pwd else: raise ConnectionError,"No active connection" # }}}1 # Method to RENAME (rename a file) {{{1 ## Renombra un fichero en el servidor ## \param self - ## \param old Antiguo nombre del fichero ## \param new Nuevo nombre del fichero ## \exception TransferError Si se produce un error durante el envío del fichero ## \exception ConnectionError Si no estamos conectados al servidor def RENAME (self,old,new): if (self.__connected): try: # Rename the file self.__stream.rename(old,new) except Exception,e: raise TransferError,"RENAME Error: %s" % e else: raise ConnectionError,"No active connection" # }}}1 # Method to UTIME (set atime and mtime from a file) {{{1 ## Dado un fichero le actualiza su atime y su mtime ## \param self - ## \param path Ruta completa hasta el fichero que deseamos actualizar ## \param utime Fecha que deseamos cargarle (en formato UTIME: "YYYYMMDDhhmm", ejemplo: "200611301639") ## \exception TransferError Si se produce un error durante el envío del fichero ## \exception ConnectionError Si no estamos conectados al servidor def UTIME (self,path,utime): # Check if we are connected if (self.__connected): # Get SITE HELP to check if this command is available in the server side site_help=self.__stream.voidcmd("SITE HELP") # Check if the server support SITE UTIME if ((len(site_help.split("UTIME")))==2): # Check of the utime string if ((len(utime))==12): try: # Change the utime of the file self.__stream.voidcmd("SITE UTIME %s %s" % (utime,path)) except Exception,e: raise TransferError,"UTIME Error: %s" % e else: # The UTIME format is not correct raise CommandError,"UTIME string should have format YYYYMMDDhhmm" else: # SITE doesn't support UTIME command raise CommandError,"Command SITE UTIME unsupported by server" else: # Not connected to the server raise ConnectionError,"No active connection" # }}}1 # EXCEPTION CLASSES {{{1 # Except (General Exception) {{{2 class Except(Exception): def __init__(self,string): self.string=string def __str__(self): return self.string #}}}2 # ConnectionError (Connection Error) {{{2 class ConnectionError(Exception): def __init__(self,string): self.string=string def __str__(self): return self.string # }}}2 # TransferError (Transfer Error) {{{2 class TransferError(Exception): def __init__(self,string): self.string=string def __str__(self): return self.string # }}}2 # CommandError (Command Error) {{{2 class CommandError(Exception): def __init__(self,string): self.string=string def __str__(self): return self.string # }}}2 # }}}1