Advertisement
renix1

main ftp

Jul 22nd, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.20 KB | None | 0 0
  1. # coding:utf-8
  2. from config import ConfigurationFile as CFG
  3. from ftplib import FTP
  4. from os import path, listdir, getcwd
  5. import hashlib
  6.  
  7.  
  8. class FTPServer(object):
  9.     """
  10.        A implementation to ftp, like FileZilla in cli
  11.    """
  12.     def __init__(self, user_data):
  13.         self._user_data = list(user_data)[0]
  14.         self._ftp = FTP('ftp.epizy.com')
  15.         self.__connect()
  16.         self.menu()
  17.         self.__exit()
  18.  
  19.     def __connect(self):
  20.         """
  21.            Create a connection to ftp server
  22.        """
  23.         print("Connecting and entering...")
  24.         print self._ftp.getwelcome()
  25.         self._ftp.login(self._user_data[0], self._user_data[1])
  26.         self._ftp.cwd('rdantas.ml/htdocs/sync_files/pdfs')
  27.         self._ftp.retrlines('LIST')
  28.  
  29.     def __exit(self):
  30.         """
  31.            Close the connection
  32.        """
  33.         self._ftp.quit()
  34.  
  35.     def upload_file(self, file):
  36.         """
  37.            Upload file to the ftp server
  38.        """
  39.         if path.exists(file) and path.isfile(file):
  40.             filename = file
  41.             self._ftp.storbinary('STOR {}'.format(filename), open(filename, 'rb'))
  42.             print('{} has uploaded'.format(file))
  43.         else:
  44.             raise Exception('No file found!')
  45.  
  46.     def download_file(self, file):
  47.         """
  48.            Get the file
  49.        """
  50.         m = hashlib.md5()
  51.         with open(file, 'wb') as f:
  52.             self._ftp.retrbinary('RETR {}'.format(file), f.write)
  53.         print('{} has downloaded'.format(file))
  54.  
  55.     def delete_file(self, file):
  56.         """
  57.            Delete a file from server
  58.        """
  59.         self._ftp.delete(file)
  60.         print('{} has deleted'.format(file))
  61.  
  62.     def get_hash_file(self, file):
  63.         """
  64.            Get md5 hash from file
  65.        """
  66.         m = hashlib.md5()
  67.         with open(file, 'rb') as f:
  68.             for b in iter(lambda: f.read(1), b''):
  69.                 m.update(b)
  70.         return m.hexdigest()
  71.  
  72.     def compare_file(self, file):
  73.         m = hashlib.md5()
  74.         with open(file, 'rb') as f:
  75.             for b in iter(lambda: f.read(), b''):
  76.                 m.update(b)
  77.         hash_cloud = m.hexdigest()
  78.         files_on_pc = listdir(getcwd())
  79.         for f in files_on_pc:
  80.             with open(f, 'rb') as ff:
  81.                 for b in iter(lambda: ff.read(), b''):
  82.                     m.update(b)
  83.             print("Cloud hash: {}\nLocal hash: {}\n".format(hash_cloud, m.hexdigest()))
  84.             if hash_cloud == m.hexdigest():
  85.                 yield file, True
  86.             else:
  87.                 yield file, False
  88.  
  89.  
  90.     def synchronize(self):
  91.         """
  92.            This function found files with md5 and download
  93.            TODO:
  94.                if compare_file return False download file to store on local
  95.  
  96.            explanation of hahs variable:
  97.                to_download[file] = (compare_result)
  98.        """
  99.         to_download = {}
  100.         files = self._ftp.nlst()
  101.         for f in files:
  102.             if not path.isdir(f):
  103.                 file, result = compare_result = self.compare_file(f)
  104.                 to_download[file] = result
  105.                 print('File {} ({})'.format(file, result))                                                                                                                                                  
  106.  
  107.     def menu(self):
  108.         """
  109.            A simple menu in cli to make actions
  110.        """
  111.         while 1:
  112.             m_option = int(raw_input('[1] - Download file\n[2] - Upload file\n[3] - Sync\n[4] - Delete file\nChoose a option: '))
  113.             if m_option == 1:
  114.                 self.download_file(raw_input("Write the filename: "))
  115.             elif m_option == 2:
  116.                 self.upload_file(raw_input("Write the filename: "))
  117.             elif m_option == 3:
  118.                 self.synchronize()
  119.             elif m_option == 4:
  120.                 self.delete_file(raw_input("Write the filename: "))
  121.             elif m_option == 5:
  122.                 print('Exiting...')
  123.                 break
  124.             else:
  125.                 print("{} option not recognized, try again...".format(m_option))
  126.  
  127. if __name__ == '__main__':
  128.     config = CFG()
  129.     f = FTPServer(config.load_user())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement