Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding:utf-8
- from config import ConfigurationFile as CFG
- from ftplib import FTP
- from os import path, listdir, getcwd
- import hashlib
- class FTPServer(object):
- """
- A implementation to ftp, like FileZilla in cli
- """
- def __init__(self, user_data):
- self._user_data = list(user_data)[0]
- self._ftp = FTP('ftp.epizy.com')
- self.__connect()
- self.menu()
- self.__exit()
- def __connect(self):
- """
- Create a connection to ftp server
- """
- print("Connecting and entering...")
- print self._ftp.getwelcome()
- self._ftp.login(self._user_data[0], self._user_data[1])
- self._ftp.cwd('rdantas.ml/htdocs/sync_files/pdfs')
- self._ftp.retrlines('LIST')
- def __exit(self):
- """
- Close the connection
- """
- self._ftp.quit()
- def upload_file(self, file):
- """
- Upload file to the ftp server
- """
- if path.exists(file) and path.isfile(file):
- filename = file
- self._ftp.storbinary('STOR {}'.format(filename), open(filename, 'rb'))
- print('{} has uploaded'.format(file))
- else:
- raise Exception('No file found!')
- def download_file(self, file):
- """
- Get the file
- """
- m = hashlib.md5()
- with open(file, 'wb') as f:
- self._ftp.retrbinary('RETR {}'.format(file), f.write)
- print('{} has downloaded'.format(file))
- def delete_file(self, file):
- """
- Delete a file from server
- """
- self._ftp.delete(file)
- print('{} has deleted'.format(file))
- def get_hash_file(self, file):
- """
- Get md5 hash from file
- """
- m = hashlib.md5()
- with open(file, 'rb') as f:
- for b in iter(lambda: f.read(1), b''):
- m.update(b)
- return m.hexdigest()
- def compare_file(self, file):
- m = hashlib.md5()
- with open(file, 'rb') as f:
- for b in iter(lambda: f.read(), b''):
- m.update(b)
- hash_cloud = m.hexdigest()
- files_on_pc = listdir(getcwd())
- for f in files_on_pc:
- with open(f, 'rb') as ff:
- for b in iter(lambda: ff.read(), b''):
- m.update(b)
- print("Cloud hash: {}\nLocal hash: {}\n".format(hash_cloud, m.hexdigest()))
- if hash_cloud == m.hexdigest():
- yield file, True
- else:
- yield file, False
- def synchronize(self):
- """
- This function found files with md5 and download
- TODO:
- if compare_file return False download file to store on local
- explanation of hahs variable:
- to_download[file] = (compare_result)
- """
- to_download = {}
- files = self._ftp.nlst()
- for f in files:
- if not path.isdir(f):
- file, result = compare_result = self.compare_file(f)
- to_download[file] = result
- print('File {} ({})'.format(file, result))
- def menu(self):
- """
- A simple menu in cli to make actions
- """
- while 1:
- m_option = int(raw_input('[1] - Download file\n[2] - Upload file\n[3] - Sync\n[4] - Delete file\nChoose a option: '))
- if m_option == 1:
- self.download_file(raw_input("Write the filename: "))
- elif m_option == 2:
- self.upload_file(raw_input("Write the filename: "))
- elif m_option == 3:
- self.synchronize()
- elif m_option == 4:
- self.delete_file(raw_input("Write the filename: "))
- elif m_option == 5:
- print('Exiting...')
- break
- else:
- print("{} option not recognized, try again...".format(m_option))
- if __name__ == '__main__':
- config = CFG()
- f = FTPServer(config.load_user())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement