def __init__(self, _configfile): """ Constructor :param _configfile: path to the configuration file. """ if not _configfile: raise Exception('Missing parameter _configfile') self.configfile = os.path.abspath(_configfile) # parse config file parameters = Helpers.get_param_from_config_file( self.configfile, ['ip', 'port', 'ftp_root_dir', 'clients'], ['client_timeout', 'pem_certificate']) self.ip = parameters['ip'] self.port = int(parameters['port']) self.root_dir = os.path.abspath(parameters['ftp_root_dir']) # use certificate ? if parameters['pem_certificate']: pem = os.path.abspath(parameters['pem_certificate']) logging.info('TLS mode use %s', pem) if not os.path.exists(pem): raise IOError('File: ' + pem + ' not found') self.handler = TLS_FTPHandler self.handler.certfile = pem else: logging.info('Unsecure mode') self.handler = FTPHandler # Use timeout ? if parameters['client_timeout']: self.handler.timeout = int(parameters['client_timeout']) if not os.path.exists(self.root_dir): logging.debug('Create %s', self.root_dir) os.makedirs(self.root_dir) self.authorizer = QuickFtpAuthorizer() for client in parameters['clients']: user_dir = os.path.join(self.root_dir, client['directory']) if not os.path.exists(user_dir): logging.debug('Create %s', user_dir) os.makedirs(user_dir) logging.info('Add user %s with directory %s', client['name'], user_dir) self.authorizer.add_user(client['name'], client['password'], user_dir, perm='elr') self.handler.authorizer = self.authorizer
def __init__(self, _configfile): """ Constructor: Read configuration file and try to connect to the server. :param _configfile: configuration file """ if not _configfile: raise Exception('Missing parameter _configfile') self.configfile = os.path.abspath(_configfile) parameters = Helpers.get_param_from_config_file(self.configfile, ['ip', 'port', 'username','password', 'workspace']) self.server_ip = parameters['ip'] self.server_port = int(parameters['port']) self.username = parameters['username'] self.password = parameters['password'] self.workspace = os.path.abspath(parameters['workspace']) self.data_dir = os.path.join(self.workspace,'data') Helpers.create_dir_if_not_exist(self.workspace) self.ftp = FTP() self.__connect()
def __get_file(self, fpath, to, verify): """ Private function to get a file. :param fpath: file path on the server :param to: local directory where the file will be copied :param verify: 'md5' or 'sha256' :return: the local file path """ logging.debug('get file "%s"' % fpath) type = self.__get_type(fpath) if type == 'dir': logging.debug('%s is a directory' % fpath) local_dir = os.path.join(to, fpath) if not os.path.isdir(local_dir): logging.debug('Create %s', local_dir) os.makedirs(local_dir) for f in self.__get_content_list(fpath): self.__get_file(os.path.join(fpath, f), local_dir, verify) return os.path.join(to, fpath) elif type == 'file': file_path = self.__get_file_from_server(fpath, to) if verify: signature_filename = fpath + '.' + verify if self.is_present(signature_filename): signature_file_path = self.__get_file_from_server(signature_filename, to) Helpers.verify_signature(file_path, signature_file_path) else: raise Exception('No %s found' % signature_filename) return file_path else: raise Exception('Unable to get type of %s' % fpath)
import argparse import logging from quickftp.qftp_helper import Helpers from quickftp.qftp_server import QuickFtpServer if __name__ == '__main__': parser = argparse.ArgumentParser(description="getLaunchServer") parser.add_argument('-v', '--verbose', action='store_true', help="verbose") parser.add_argument('-cf', '--config-file', dest="configfile", help="config file") args = parser.parse_args() if args.verbose: Helpers.configure_logger(logging.DEBUG) else: Helpers.configure_logger(logging.INFO) server = QuickFtpServer(args.configfile) server.serve()
def server_handler(cls): Helpers.configure_logger(logging.ERROR) current_dir_path = os.path.dirname(os.path.realpath(__file__)) s = QuickFtpServer( os.path.join(current_dir_path, 'conf/server_conf.yml')) s.serve()