def __init__(self, secured=False, verbose=False): """Class constructor Called when the object is initialized Args: secured (bool): secured FTP verbose (bool): verbose mode """ self._mh = MasterHead.get_head() self._secured = secured if (not self._secured): self._client = FTP() else: if (not(version_info[0] == 2 and version_info[1] == 6)): self._client = FTP_TLS() else: raise NotImplementedError( 'Secured mode is not supported for Python 2.6') self._verbose = verbose if (self._verbose): self._client.set_debuglevel(2)
def doRequest(): # Send the file up to the FTP server print ("Upload picture start") ftp = FTP_TLS('individual.utoronto.ca', 'USER','1PASS.') file = open('data.jpg','rb') # file to send ftp.storbinary('STOR data.jpg', file) # send the file file.close() # close file and FTP ftp.quit() print ("Upload picture complete") print ("Requesting file request") reqUrlA = 'http://api.cloudsightapi.com/image_requests/' # get token reqUrlB = 'http://api.cloudsightapi.com/image_responses/' # get the final recognition result with token headers = { 'Authorization' : 'CloudSight 149xzcR0nYPrwThNXVLecQ', } postData = { 'image_request[remote_image_url]' : "http://individual.utoronto.ca/timlock/data.jpg", 'image_request[locale]': "en_us", 'image_request[language]': "en" } try: response = requests.post(reqUrlA, headers=headers, params=postData) except Exception, e: print 'Error: connection error, please check your Internet and confirm the image url' print e return ("FAILED")
def send_chunks(self): if self.final_chunks is None: return ERR if self.tls_flag: if self.auth_flag: ftp_obj = FTP_TLS(host=self.server, user=self.creds[0], passwd=self.creds[1]) else: ftp_obj = FTP_TLS(host=self.server) else: if self.auth_flag: ftp_obj = FTP(host=self.server, user=self.creds[0], passwd=self.creds[1]) else: ftp_obj = FTP(host=self.server) try: ftp_obj.login() sys.stdout.write("\t[+]\tConnected to server %s.\n" % self.server) except: sys.stderr.write("\t[-]\tCould not login to the server.\n") return ERR for chunk in self.final_chunks: ftp_obj.mkd(chunk) time.sleep(SLEEP) ftp_obj.quit() sys.stdout.write("\t[+]\tWrote %s(+1) folders.\n" % (len(self.final_chunks)-1)) return OKAY
def __init__(self,ftp_server,username,password,port,timeout=10,acct='', keyfile=None,certfile=None,): FTP_TLS.__init__(self, '', '','', acct, keyfile, certfile, timeout) self.ftp_server = ftp_server self.username = username self.password = password self.port = port self.timeout = timeout self.ftp = None
def connect(): global ftp; if remoteTLS: context = ssl.create_default_context(); ftp = FTP_TLS(remoteHost, remoteUser, remotePassword, acct="", keyfile=None, certfile=None, context=context, timeout=20); ftp.prot_p(); else: ftp = FTP(remoteHost, remoteUser, remotePassword, 20); print(ftp.getwelcome());
def connect(self): ftps = FTP_TLS(self.host) ftps.login(self.username, self.passwd) # switch to secure data connection.. # IMPORTANT! # Otherwise, only the user and password is encrypted and not all the file data. ftps.prot_p() return ftps
def connect(self): self.sock = socket.create_connection((self.ftp_server, self.port), self.timeout) self.af = self.sock.family self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile,ssl_version=ssl.PROTOCOL_TLSv1) self.file = self.sock.makefile('rb') self.welcome = self.getresp() self.login(self.username,self.password) FTP_TLS.prot_p(self) return self.welcome
def get_file(filename): # how do we 'stream' the file from Box to browser? using a callback! class VMFile: # this will store the VM message as a def __init__(self): # memory object instead of in a file (+ deleted after execution) self.data = "" def __call__(self,s): self.data += s v = VMFile() session = FTP_TLS('ftp.box.com', box_username, box_password) # open Box session.retrbinary('RETR recordings/' + filename, v) # add each chunk of data to memory from Box session.close() # close Box return v.data # return the data put back together again to be sent to browser
def storeFtplib(dataframe, filename="cameliaBalAGKevin.csv", compression = None, toPrint = False): """ function that connects to the remote FTP serveur and upload a pandas dataframe the upload file must be a pandasDataframe and will be written in a csv file. It can be uploaded as a bz2, gz encoded or not encoded at all if it is encoded, the right extension must be present in the name -- IN dataframe : the dataframe to upload (pandas.Dataframe) filename : the filename with its extension to be downloaded from the remote ftp server (string) compression : string that specifies the encoding of the file (string in [None,"gz","bz2"] default: None toPrint : boolean that settles if the function should print its progress and results (boolean) default: False -- OUT flag : boolean that settles if everything was successful (True: no problem, False: an error occured) """ startTime = time.time() if toPrint: print "" print "" print "===========================================" print "=== Connection to the remote FTP server ===" print "===========================================" print "" print "using ftplib" print "loading :",filename print "" ftp = FTP_TLS() # retrieving information about account on ftp server (user, password, host, port) = getAccount() if user==None: print "error : coudn't read the account information" return False # connecting and logging in try: ftp.connect(host,port) ftp.login(user,password) except: print "error : unable to connect to the ftp server" return False # establishing the security protocol ftp.prot_p() if toPrint: print "connected to the FTP server" try: lines = dataframe.to_csv(path_or_buff = None,sep="\t",columns=dataframe.columns) except: print "error : impossible to convert the dataframe into csv lines" return False sio = StringIO.StringIO(lines) ftp.storlines(cmd="STOR "+filename, fp=sio) # try: # ftp.storlines(cmd="STOR "+filename, file=lines) # except: # print "error : impossible to upload the file" # return False interval = time.time() - startTime if toPrint: print 'Dataframe uploaded :', interval, 'sec' return True
def main(): global ftp_client scan_arguments() ftp_client = FTP(host) try: ftp_client.login(username, password) except ftplib.all_errors as e: print "ERROR: cannot login with username '{0}' and relative password.\nMessage returned from server:".format(username) print e return try: ftp_client.cwd(remote_dir) except ftplib.all_errors as e: print "ERROR: emote directory '{0}' not existing.\nMessage returned from server:".format(remote_dir) print e return else: files = ftp_client.nlst() print_directory_content(files) setup_folder() download_files(remote_dir, files) if compress: create_zip(local_dir) try: ftp_client.close() print "!!!!! OPERATION COMPLETED SUCCESSFULLY !!!!!" except ftplib.all_errors as e: print "ERROR: cannot close the connection properly.\nMessage from server:" print e
def start(self): # create connection for ftpserver, users in self.db.iteritems(): for s_user in users: self.log.log("Connecting to %s: user: %s pass: %s" % (ftpserver, s_user.user, s_user.passwd)) ftp = FTP_TLS(ftpserver) # connect to host, default port ftp.login(user=s_user.user, passwd=s_user.passwd) ftp.prot_p() # switch to secure data connection ftp.retrlines('LIST', self.parseFiles) self.log.log("Done! now quit...") ftp.quit()
def test_connection(self, cr, uid, ids, context={}): for ftp in self.browse(cr, uid, ids, context=context): conn = False try: # Perhaps to include timeout? conn = FTP_TLS(host=ftp.host, user=ftp.username, passwd=ftp.passwd) except: conn = FTP(host=ftp.host, user=ftp.username, passwd=ftp.passwd) if not conn: raise osv.except_osv(('Error!'), ("Connection can not be established!\nPlease, check you settings")) conn.quit() raise osv.except_osv(('!'), ("Connection Succeed!"))
def test_connection(self, cr, uid, ids, context={}): for ftp in self.browse(cr, uid, ids, context=context): conn = FTP_TLS( host=ftp.host, user=ftp.username, passwd=ftp.passwd ) if not conn: raise orm.except_orm( _('Error!'), _("Connection can not be established!\n" "Please, check you settings") ) conn.quit() raise orm.except_orm( _('!'), _("Connection Succeed!") )
class FTPS: def __init__(self): self.ftps = FTP_TLS( ) def connect(self): self.ftps.connect('192.168.0.102', 2121) print(self.ftps.getwelcome()) def login(self): self.ftps.login('anderson', 'nosredna89') self.ftps.prot_p() #para fazer a conexação de dados segura def close(self): self.ftps.close()
def write_file(self): output = open('chan_output.htm', 'w') for channel in self.main.channels: self.logger.log(LOG_DEBUG, "Writing output for %s, topic %s" % (channel, self.topics[channel])) output.write("<b>Channel:</b> %s\n<br /><b>Topic:</b> %s\n<br /><b>Users:</b>\n<ul>\n" % (channel, self.topics[channel])) for user in self.main.channels[channel]['users']: output.write(" <li>%s</li>\n" %(user)) output.write("</ul>\n\n") output.close output = open('chan_output.htm', 'r') self.update_all = 0 ftp_server = 'whub25.webhostinghub.com' ftp_user = '******' passfile = open('password.txt','r') password = passfile.readline() passfile.close() password = password.rstrip('\n') self.logger.log(LOG_INFO, "Connecting to FTP server %s, username %s" % (ftp_server, ftp_user)) ftp = FTP_TLS(ftp_server, ftp_user, password) ftp.prot_p() self.logger.log(LOG_INFO, "Successfully logged in, storing file") ftp.storlines('STOR chan_output.htm', output) ftp.close() output.close() self.update_all = 0 self.logger.log(LOG_INFO, "Done")
def ftp_backup(dmpdir,dbname): try: # ftp =FTP_TLS() ftp.connect(ftphost,ftpport) ftp.login(ftpuser,ftppass) ftp.prot_p() print "Welcome:",ftp.getwelcome() print ftp.retrlines('LIST') # ftp =FTP() # ftp.connect(ftphost,ftpport) # ftp.login(ftpuser,ftppass) # print "Welcome:",ftp.getwelcome() # print ftp.retrlines('LIST') except Exception,e: print e return
def import_to_ftp(): import pdb pdb.set_trace() with FTP_TLS("ftp.cloud.viewpoint.com") as ftps: ftps.login("945", "QnHS6468") ftps.prot_p() ftps.cwd(r"\imports\EM\SkyBitzMtr") local_path = r"C:\Users\jjacinto\OneDrive - HSI\Documents\GitHub\Skybitz_Vista\official_sky_bits_project\official_sky_bits_project\skybitz_2020102.csv" with open(local_path, "rb") as csv_file: ftps.storlines("Stor skybitz_2020102.csv", csv_file) ftps.dir()
def upload_file_with_ftp_over_ssl(): yesterday = (datetime.today() - timedelta(days=1)).date().__str__() for _path in Path("html").glob("*"): if ("2021" not in _path.name) or (yesterday in _path.name): local_path = "html/" + _path.name print("uploading..\t", local_path) remote_path = f"twitter_aichi_covid19/{local_path.split('/')[-1]}" with FTP_TLS(host=host_name, user=user_name, passwd=password) as ftp: with open(local_path, 'rb') as f: ftp.storbinary(f'STOR {remote_path}', f)
def _connect(self): """Connect to FTP server """ if self.conn is not None: return logging.debug('Creating new connection to server %s', self.server) not_connected = True attempt = 1 while not_connected and attempt < 6: try: self.conn = FTP_TLS(self.server, timeout=self.timeout) self.conn.login(self.username, self.password) self.conn.prot_p() not_connected = False except Exception as exp: logging.debug(exp) time.sleep(5) self.close() attempt += 1 if not_connected is True: raise Exception("Failed to make FTP connection after 5 tries!")
def connect(self): """ Try to connect to the FTP server. Raise an error if something went wrong. """ try: self.ftps = FTP_TLS() self.ftps.set_pasv(True) self.ftps.connect(self._host, self._port) except socket.gaierror: raise
def login(self, debug=2, set_pasv=True): _old_makepasv = FTP_TLS.makepasv def _new_makepasv(self): host, port = _old_makepasv(self) host = self.sock.getpeername()[0] return host, port FTP_TLS.makepasv = _new_makepasv self.Ftp = FTP_TLS(self.host) self.Ftp.set_debuglevel(debug) self.Ftp.auth() self.Ftp.login(self.user, self.pwd) self.Ftp.makepasv() self.Ftp.sendcmd('pbsz 0') self.Ftp.set_pasv(set_pasv) self.Ftp.prot_p() print("您好 您已经登录 ftp: %s 服务器" % self.host) self.Ftp.getwelcome() return self.Ftp
def Push( FtpServer, Username, Password, uploadlist = FilesToPut, port = 21): print >> sys.stderr, "Login to %s:%s using %s:%s"%(FtpServer, port, Username, 'xxx') ftp = FTP() ftps = FTP_TLS() ftps.connect(FtpServer,Port) ftps.auth() ftps.login(Username, Password) # login anonymously before securing control channel ftps.prot_p() ftp.set_pasv(False) for f in uploadlist: #print "uploading %s"%f fp = open( f, 'rb') os.path.basename(f) print f ftp.storbinary("STOR,%sx " %(os.path.basename(f),fp)) #ftp.storbinary('STOR, %s',fp %(basename(f)) ) # send the file ftp.quit()
def connect(self): if(self.readConfig()): try: self.ftp = FTP_TLS(self.server) password = self.__askPassWord() self.ftp.login(self.user, password) #self.ftp.prot_p() return True except Exception, e: print(color.TFC + "ftc " + color.WARNING + "Unable to log in...") self.__writeConfig("Config", "time", 0) return False
def lambda_handler(event, context): ExtraArgs = { 'ServerSideEncryption': 'aws:kms', 'SSEKMSKeyId': 'alias/<alias_name>' } s3 = s3fs.S3FileSystem(anon=False, s3_additional_kwargs=ExtraArgs) ftp_url = "local" #provide FTP host ftp_path = "/test_folder/" #provide FTP path s3Bucket = "s3-bucket" #provide s3 bucket name file_name = "sample.txt" #provide file name ftps = FTP_TLS(ftp_url) ftps.login('<user_name>', '<pwd>') ftps.prot_p() logger.info('Login Successful') ftps.cwd(ftp_path) logger.info('Downloading file: ' + file_name) ftps.retrbinary('RETR ' + file_name, s3.open("{}/{}".format(s3Bucket, file_name), 'wb').write) logger.info('Download completed: ' + file_name)
def connectFTPS(hst, usr, pwd, prt): try: ftps = FTP_TLS(hst, usr, pwd) retr = ftps.retrlines('SYST') retr.join(ftps.retrlines('PWD')) retr.join(ftps.retrlines('LIST')) ftps.quit() return retr except Exception as e: return "%s" % (e)
def doRequest(): # Send the file up to the FTP server print("Upload picture start") ftp = FTP_TLS('individual.utoronto.ca', 'USER', '1PASS.') file = open('data.jpg', 'rb') # file to send ftp.storbinary('STOR data.jpg', file) # send the file file.close() # close file and FTP ftp.quit() print("Upload picture complete") print("Requesting file request") reqUrlA = 'http://api.cloudsightapi.com/image_requests/' # get token reqUrlB = 'http://api.cloudsightapi.com/image_responses/' # get the final recognition result with token headers = { 'Authorization': 'CloudSight 149xzcR0nYPrwThNXVLecQ', } postData = { 'image_request[remote_image_url]': "http://individual.utoronto.ca/timlock/data.jpg", 'image_request[locale]': "en_us", 'image_request[language]': "en" } try: response = requests.post(reqUrlA, headers=headers, params=postData) except Exception, e: print 'Error: connection error, please check your Internet and confirm the image url' print e return ("FAILED")
def sauvegarde(self): lg = logging.getLogger() lg.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s;%(levelname)s;%(message)s;') file_handler = FileHandler('logs/' + datetime.now().strftime("%Y%m%d-%H-%M-%S") + '.log', 'a') file_handler.setLevel(logging.INFO) file_handler.setFormatter(formatter) lg.addHandler(file_handler) lg.info('Start Log') if not os.path.isdir('temp'): os.mkdir('temp') lg.info('Temp folder created') splogs = Thread(None, supplogs(lg)) flt = self.search([])[0].filestore_path flstr = Thread(None, filestoring(lg,flt)) db = str(self._cr.dbname) dbdp = Thread(None, dbdumping(lg, db)) splogs.start() flstr.start() dbdp.start() flstr.join() dbdp.join() splogs.join() lg.info('Start of the transfer') ftp = FTP_TLS() ftp.set_debuglevel(2) sauvegardes = self.search([]) for sauvegarde in sauvegardes: if sauvegarde.active: multiple_transfer(lg, ftp, db, sauvegarde)
def connect_ftp(): #Connect to the server ftp = FTP_TLS() ftp.connect(SERVER, PORT) ftp.login(USER, PASS) return ftp
def connect(box_path): ftp = FTP_TLS('ftp.box.com') # ftp.debugging = 2 ftp.login('*****@*****.**', 'cJ19870915@#') ftp.cwd(box_path[0]) print("Connected to Box FTP") return ftp
def __connectTls(self, host, user, password): ftp_tls = FTP_TLS(host) ftp_tls.login(user, password) ftp_tls.prot_p() return ftp_tls
class ExplicitTLS(Configuration): """ This class includes methods to allow establishing a secure Explicit FTP secure connection to the One Scotland Gazetteer FTP """ def __init__(self): Configuration.__init__(self) self.host = self.get_configuration_for('ftp', 'host') self.port = int(self.get_configuration_for('ftp', 'port')) self.username = self.get_configuration_for('ftp', 'username') self.password = self.get_configuration_for('ftp', 'password') def setup(self): # An FTP subclass which adds TLS support to FTP self.client = FTP_TLS(timeout=10) def connect(self): self.client.connect(host=self.host, port=self.port) def login(self): self.client.login(user=self.username, passwd=self.password) #Make our connection to the server secure (i.e. encrypted) self.client.prot_p() #This is a hack making 'ftplib' use the EPSV network protocol (i.e. an IPv6 connection) instead of the PASV #protocol (i.e. an IPv4). The reason for doing this is that there is a bug in FTP lib which returns the wrong #IP address after connection to the FTP if PASV is used. In contrast if the EPSV protocol is used the FTP IP is #returned correctly allowing further commands to the FTP connection. self.client.af = socket.AF_INET6 return self.client
def onLogin(self, username, passwd): ok = True msg = '' error_msg = 'Login failed.' try: if not self.ftp: self.ftp = FTP_TLS(self.host) if self.useSSL is True else FTP( self.host) loginResponse = self.ftp.login(username, passwd) except socket.gaierror: self.ftp = None ok = False msg = 'Server address could not be found.' except (error_perm, error_reply): info = traceback.format_exception(*sys.exc_info()) for i in info: sys.stderr.write(i) ok = False msg = error_msg else: if '230' in loginResponse: ok = True else: ok = False msg = error_msg if ok: # Logged in. Now let's do compability tests. if not self.testPermissions(): # User doesn't have write permissions, don't bother doing next test. ok = False msg = 'It seems like you do not have write access to this server.' else: # Permissions test passed, now let's test MFMT for timestamp modification. if not self.testMFMT(): ok = False msg = 'This server does not support timestamp modification\n \ need by this application.' self.loginCompleted.emit(ok, msg)
def _DownloadFTPFile(fname): ''' Downloads a file from an FTP site, returns the full path of the local version of that file. Inputs: addr: full address of file to be downloaded e.g. ftp://a.b.c/folder/file.txt fname: file name e.g. file.txt Returns: full path to downloaded file ''' #login to the FTP server ftp = FTP_TLS(Globals.ftpbase) ftp.login() ftp.cwd(Globals.ftpdir) #open the output file f = open(Globals.DataPath + 'tmp/' + fname, "wb") #get the callback function global progress progress = 0 cb = _GetCallback(f, ftp, fname) #download binary file using ftplib print('Downloading: {:s}'.format(fname)) ftp.retrbinary('RETR ' + fname, cb) print() #close the file f.close() #close FTP connection ftp.close() #return the file name return Globals.DataPath + 'tmp/' + fname
def backup_to_ftp(upload_db_backup=True): if not frappe.db: frappe.connect() # upload database ftp_settings, use_tls, root_directory, file_backup, limit_no_of_backups, no_of_backups = get_ftp_settings() if not ftp_settings['host']: return 'Failed backup upload', 'No FTP host! Please enter valid host for FTP.' if not ftp_settings['user']: return 'Failed backup upload', 'No FTP username! Please enter valid username for FTP.' if not root_directory: return 'Failed backup upload', 'No FTP username! Please enter valid username for FTP.' ftp_client = FTP_TLS(**ftp_settings) if use_tls else FTP(**ftp_settings) try: if upload_db_backup: backup = new_backup(ignore_files=True) filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db)) upload_file_to_ftp(filename, combine_path(root_directory, "/database"), ftp_client) # delete older databases if limit_no_of_backups: delete_older_backups(ftp_client, combine_path(root_directory, "/database"), no_of_backups) # upload files to files folder did_not_upload = [] error_log = [] if file_backup: upload_from_folder(get_files_path(), 0, combine_path(root_directory, "/files"), ftp_client, did_not_upload, error_log) upload_from_folder(get_files_path(is_private=1), 1, combine_path(root_directory, "/private/files"), ftp_client, did_not_upload, error_log) return did_not_upload, list(set(error_log)) finally: ftp_client.quit()
def connect(self, context): ui_props = context.scene.editAsset ftp = FTP_TLS() ftp.connect('ftp.luxcorerender.org', 21) ftp.login(ui_props.username, ui_props.password) return ftp
def connect(self): self.ensure_one() if self.tls: ftp_conn = FTP_TLS(timeout=15) else: ftp_conn = FTP(timeout=15) try: ftp_conn.connect(host=self.host, port=self.port) ftp_conn.login(user=self.user, passwd=self.password) if self.tls: ftp_conn.prot_p() except Exception as exc: raise Warning(_('FTP Error'), _('Could not connect to FTP Server\n\n%s') % exc) return ftp_conn
def __init__(self): self.name = "" self.commands = {} self.commands_function = {} self.user = "" self.server = "" self.password = "" self.port = 0 self.copy_dir = "" self.dest = "" self.assets = "" self.ftp = FTP_TLS() self.folder = ".tfc" self.config_file = "config.ini" self.config_path = self.folder + os.path.sep + self.config_file self.config_file_content = "" self.db_name = "ftp_files.db" self.db_path = self.folder + os.path.sep + self.db_name self.place_holder_config = "files/config.ini" self.help_path = "files/help.json" self.time_out = 300 self.time = 0
def connect(self, host, port, tls, timeout): if tls == '0': fp = FTP(timeout=int(timeout)) else: fp = FTP_TLS(timeout=int(timeout)) banner = fp.connect(host, int(port)) if tls != '0': fp.auth() return TCP_Connection(fp, banner)
def doLogin(self, user, passwd, proxy=None): #Installing SOCK proxy if proxy: import socket try: import socks except ImportError: logging.critical("sock module not installed.") return ip, port = proxy.split(":") socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, ip, int(port)) socket.socket = socks.socksocket try: ftps = FTP_TLS(self.address, timeout=20) #Connecting except socket.error as errno: logging.debug(errno) return "PROXY FAIL" try: ftps.login(user, passwd) #Try to login except Exception: logging.debug("{0}:{1} failed.".format(user, passwd)) return None logging.info("Valid user found. {0}:{1}".format(user, passwd)) if self.options.get("output_file"): try: output_file = open(self.options.get("output_file"), "a") output_file.write("{0}:{1}".format(user, passwd)) output_file.close() except IOError as errno: logging.warning(errno) finally: return user, passwd
def upload_files(self): if configBackup["FTP_BACKUP"]: # Check to make server is up, if yes back up files! Test s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(5) try: s.connect((configBackup["FTP_HOST"], 21)) listdir = os.listdir(configBackup["TMP_FOLDER"]) ftps = FTP_TLS(configBackup["FTP_HOST"]) ftps.login(configBackup["FTP_USER"], configBackup["FTP_PASSWORD"]) ftps.set_pasv(True) for item in listdir: print('Upload: ' + item) ftps.storbinary( 'STOR ' + item, open(configBackup["TMP_FOLDER"] + item, 'rb'), 1024) ftps.quit() except socket.error as e: print("Error on connect") s.close() if configBackup["SFTP_BACKUP"]: try: listdir = os.listdir(configBackup["TMP_FOLDER"]) with pysftp.Connection( configBackup["SFTP_HOST"], username=configBackup["SFTP_USER"], password=configBackup["SFTP_PASSWORD"]) as sftp: for item in listdir: print('Upload: ' + item) sftp.put(configBackup["TMP_FOLDER"] + item) except socket.error as e: print('Error on connect')
def connect(self): print(f"{self.server} {self.port} {self.usrname} {self.pwd}") ftp = FTP_TLS() try: ftp.connect(self.server, self.port) ftp.login(self.usrname, self.pwd) except: raise IOError('\n FTP login failed!!!') else: print(ftp.getwelcome()) print('\n+------- FTP connection successful!!! --------+\n') return ftp
def linkFtp(folder=''): """ Handlser FTP per il caricamento dei dati su server remoto :param folder: sotto cartella di salvataggio remota :return: handler per la gestione """ # ftp = FTP_TLS() ftp = FTP_TLS(host=rl['host']) ftp.set_debuglevel(2) status = ftp.login(user=rl['username'], passwd=rl['password']) # comando per il cambio della directory di upload ftp.cwd(backup_folder+'/'+folder) # print the content of directory print(ftp.dir()) return ftp, status
def _ftp_connect(login_info): url, user, passw = login_info context = ssl.create_default_context() context.set_ciphers("ALL:@SECLEVEL=1") ftps = FTP_TLS( host=os.environ[url], user=os.environ[user], passwd=os.environ[passw], context=context, ) return ftps
def lambda_handler(event, context): ExtraArgs={'ServerSideEncryption':'aws:kms','SSEKMSKeyId':'alias/<alias_name>'} s3 = s3fs.S3FileSystem(anon=False,s3_additional_kwargs=ExtraArgs) secret_name = "<secret_name>" #provide secret name from AWS secrets manager region_name = "<region_name>" #provide region name ftp_url = "localhost" #provide FTP host ftp_path = "/test_folder/" #provide FTP path s3Bucket = "s3-bucket" #provide s3 bucket name file_name = "sample.txt" #provide file name # Create a Secrets Manager client session = boto3.session.Session() client = session.client( service_name='secretsmanager', region_name=region_name ) get_secret_value_response = client.get_secret_value( SecretId=secret_name ) if 'SecretString' in get_secret_value_response: secret = get_secret_value_response['SecretString'] else: secret = base64.b64decode(get_secret_value_response['SecretBinary']) secret_dict = json.loads(secret) ftps = FTP_TLS(ftp_url) ftps.login(secret_dict['username'],secret_dict['password']) ftps.prot_p() logger.info('Login Successful') ftps.cwd(ftp_path) ftps.retrbinary('RETR ' +file_name , s3.open("{}/{}".format(s3Bucket, file_name), 'wb').write) logger.info('Download completed: ' +file_name)
def __ftp_call(self, func, **kwargs): if self.__tls: with FTP_TLS(host=self.__host, timeout=self.__timeout) as ftp: ftp.set_debuglevel(1) ftp.login(user=self.__user, passwd=self.__password) ftp.prot_p() return func(ftp=ftp, **kwargs) else: with FTP() as ftp: ftp.set_debuglevel(1) ftp.connect(host=self.__host, port=self.__port, timeout=self.__timeout) ftp.login(user=self.__user, passwd=self.__password) return func(ftp=ftp, **kwargs)
def connect(self): ftp = FTP_TLS() # using TLS version 1.2 # ftp.ssl_version = ssl.PROTOCOL_TLSv1_2 # ftp.debugging = 2 try: ftp.connect(self.url, 21) except: return "hosterr" else: try: ftp.login(self.krzl, self.pw) except: return "loginerr" else: ftp.prot_p() return ftp
class FtpHelper: def __init__(self, host, port, user, password): self.ftp = FTP_TLS() self.ftp.connect(host=host, port=int(port)) self.ftp.login(user=user, passwd=password) self.ftp.prot_p() def list_files(self, path=None): """ Lister les fichiers présents dans un dossier Parameters ---------- path: Path vers le dossier Return Liste des fichier présents dans le dossier ------- """ if Config.get_secret('WORKING_ENV') == 'LOCAL': return [file for file in os.listdir(Config.get_secret('ZIP_FILES_PATH'))] files = [] if path is not None: self.ftp.cwd(path) self.ftp.dir(files.append) return [filename.split(' ')[-1] for filename in files]
def copy_files(args): password = os.getenv('CODE_DEPLOY_PASSWORD', None) if password is None: password = getpass.getpass() file = None with FTP_TLS(host=args.host, user=args.user, passwd=password) as ftp: ftp.set_debuglevel(args.debug_level) if args.xdt_transform is not None: ftp.cwd(xdt_path) file = open(args.xdt_transform, 'rb') ftp.storbinary('STOR applicationHost.xdt', file) ftp.cwd(files_path) clean_files(ftp) place_files(ftp, args.deploy_dir) ftp.quit()
def ftps(exit): try: f = FTP_TLS(args.hostname) f.login(user=p_file.get('f_user'), passwd=p_file.get('f_pass')) f.quit() print(f'OK: FTPs login successful') return OK except: print(f'WARNING: FTPs service was not authorized') return CRITICAL
def uploadToFtp(fileList, remoteDir, host, user, password): """ :type fileList: list :type remoteDir: basestring :type host: basestring :type user: basestring :type password: basestring """ ftps = FTP_TLS(host) ftps.sendcmd('USER %s' % user) ftps.sendcmd('PASS %s' % password) for fileItem in fileList: fileName = os.path.split(fileItem)[1] remoteFilePath = os.path.join(remoteDir, fileName) print('Uploading file [{0}] to ftp at [{1}]'.format(fileName, remoteFilePath)) ftps.storbinary('STOR {0}'.format(remoteFilePath), open(fileItem)) print('Done.') ftps.quit()
class Sftpimpl(Ftpimpl): def __init__(self): self.log = ProcessLogger() def connectFTP(self,host='',port='',uname='',upass='',ssh_host_key='',acv_pcv=''): if host=='' or port=='' or uname=='' or upass=='': raise FTP_ERROR('Oops: Blank config parameters, Please dont leave any config parameter blank') self.host = host log_id = self.log.processlog(logid=0,f1=False,f2=False,hostnm=host,up_dw='login',typ='new',status='Pending',result='trying to connect...') hostStr = '%s:%s' %(host,port) usrHostStr = '%s@%s' %(uname,hostStr) try: self.ftp = FTP_TLS(hostStr) if acv_pcv == 'Active': self.ftp.set_pasv(False) elif acv_pcv == 'Passive': self.ftp.set_pasv(True) self.ftp.login(uname,upass) self.ftp.prot_p() return_msg = self.ftp.getwelcome() self.log.processlog(logid=log_id,f1=False,f2=False,hostnm=host,up_dw='login',typ='edit',status='Done',result=return_msg) except Exception as e: self.log.processlog(logid=log_id,f1=False,f2=False,hostnm=host,up_dw='login',typ='edit',status='Failed',result=str(e)) raise FTP_ERROR('Connection error: Reasons \n\n(Internet connection)\n(Remote server down)\n(Config settings) ')
def _connect(self): """Connect to FTP server """ if self.conn is not None: return logging.debug("Creating new connection to server %s", self.server) not_connected = True attempt = 1 while not_connected and attempt < 6: try: self.conn = FTP_TLS(self.server, timeout=self.timeout) self.conn.login(self.username, self.password) self.conn.prot_p() not_connected = False except Exception as exp: logging.debug(exp) time.sleep(5) self.close() attempt += 1 if not_connected is True: raise Exception("Failed to make FTP connection after 5 tries!")
def startProtocol(port, user, passwd, ip, regex, target_path, args): global num_files, size, mirror, verbose, timeout num_files = 0 size = 0 mirror = args.search verbose = args.verbose timeout = args.timeout try: print(70*'#') print('Trying FTPS server: %s:%s' %(ip, port)) host = FTP_TLS(str(ip), timeout = timeout) host.login(user, passwd) host.prot_p() print('Connected... Downloading files...\n') files = downloadFTPFiles(target_path, host, regex) print('%i files (%s) downloaded... Closing connection...' % (files, convert_bytes(size))) host.close() except ftplib.all_errors as err: print (err)
def onLogin(self, username, passwd): ok = True msg = '' error_msg = 'Login failed.' try: if not self.ftp: self.ftp = FTP_TLS(self.host) if self.useSSL is True else FTP(self.host) loginResponse = self.ftp.login(username, passwd) except socket.gaierror: self.ftp = None ok = False msg = 'Server address could not be found.' except (error_perm, error_reply): info = traceback.format_exception(*sys.exc_info()) for i in info: sys.stderr.write(i) ok = False msg = error_msg else: if '230' in loginResponse: ok = True else: ok = False msg = error_msg if ok: # Logged in. Now let's do compability tests. if not self.testPermissions(): # User doesn't have write permissions, don't bother doing next test. ok = False msg = 'It seems like you do not have write access to this server.' else: # Permissions test passed, now let's test MFMT for timestamp modification. if not self.testMFMT(): ok = False msg = 'This server does not support timestamp modification\n \ need by this application.' self.loginCompleted.emit(ok, msg)
from ftplib import FTP_TLS ftps = FTP_TLS('127.0.0.1:8021','user','12345',keyfile='privateKey.key') ftps.login() # login anonymously before securing control channel ftps.prot_p() # switch to secure data connection print ftps.retrlines('LIST')
def Push( FtpServer, Username, Password, uploadlist = FilesToPut, port = 21, passive = False, StartTls = False, Sftp = False ): print "Login to %s:%s using %s:%s (%s%s)"%(FtpServer, port, Username, 'xxx', 'passive' if passive else 'active', '/tls' if StartTls else '') if Sftp: paramiko.util.log_to_file('/tmp/paramiko.log') transport = paramiko.Transport((FtpServer,int(port))) transport.connect(username = Username, password = Password) sftp = paramiko.SFTPClient.from_transport(transport) print "Uploading file" filepath = '../php/basedata.php' localpath = 'basedata.php' sftp.put(filepath, localpath) sftp.close() transport.close() else: if StartTls: ftp = FTP_TLS() else: ftp = FTP() ftp.connect( FtpServer, port ) ftp.login( Username, Password) ftp.set_pasv( passive ) if StartTls: ftp.prot_p() for f in uploadlist: print "uploading %s"%f fp = open( f, 'rb') ftp.storbinary('STOR %s'%os.path.basename(f), fp) # send the file ftp.quit() if __name__ == "__main__": if len(sys.argv) < 5: print >> sys.stderr, "usage %s <server> <port> <username> <password>"%sys.argv[0] exit( 1 ) FtpServer = sys.argv[1] Port = sys.argv[2] Username = sys.argv[3] Passwd = sys.argv[4] Push( FtpServer, Username, Passwd, port = Port ) print >> sys.stderr, "Done"
def testFTPLibConnection(self): ''' tests if the ftplib connection is available with the FTP server ''' ftp = FTP_TLS() (user, password, host, port) = getAccount() ftp.connect(host,port) ftp.login(user,password)
def __init__(self, host='', user='', passwd='', acct='', keyfile=None, certfile=None, timeout=60): FTP_TLS.__init__(self, host, user, passwd, acct, keyfile, certfile, timeout)