Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- """
- """
- import logging
- import argparse
- import time
- from ftplib import FTP
- _logger = logging.getLogger(__name__)
- def snap_to_ftp(ftp_client, duration, interval, image_format):
- """Script to save a screenshot on a ftp server"""
- if not ftp_client: # todo: Improve the check?
- raise ValueError('A valid ftp client must be passed.')
- print('Run the script for %4ds seconds with interval %3ds' % (duration, interval))
- passed_time = 0
- snapshots = []
- while passed_time < duration:
- snapshot = get_snapshot(image_format)
- _logger.debug('Generated the snapshot: %r', snapshot)
- snapshots.append(snapshot)
- try:
- upload_file(ftp_client, snapshot)
- except Exception as e:
- _logger.exception('Failed to upload snapshot:%s', snapshot)
- time.sleep(interval)
- passed_time += interval
- def get_snapshot(image_format):
- """Returns the file object format"""
- # im.show()
- # ImageGrab.grab_to_file(str(datetime.datetime.now()).split('.')[0].replace(' ', '') + '.png')
- file = None
- return file
- def get_ftp_client(user, password, host):
- """Create, set and return the ftp client"""
- ftp = FTP(host=host, user=user, passwd=password)
- return ftp
- def upload_file(ftp_client, file_to_upload, prefix='STOR', set_pasv=True):
- """Upload the file using the given ftp_client"""
- ftp_client.set_pasv(set_pasv)
- ftp_file = open(file_to_upload, 'rb')
- ftp_client.storbinary(prefix + file_to_upload.name, ftp_file)
- ftp_file.close()
- if __name__ == '__main__':
- # setup the parser for the arguments of the script
- parser = argparse.ArgumentParser(description='Process the script arguments')
- parser.add_argument('--duration', dest='duration', type=int, default=300,
- help='Duration of the script in seconds. (default=300)')
- parser.add_argument('--interval', dest='interval', type=int, default=60,
- help='Interval in seconds between every screenshot (default=60)')
- parser.add_argument('--image-format', dest='image_format', type=str, default='png',
- help='Image format (default:png)')
- parser.add_argument('--password', dest='password', type=str, help='FTP password', required=True)
- parser.add_argument('--user', dest='ftp_user', type=str, help='FTP user', required=True)
- parser.add_argument('--host', dest='ftp_host', type=str, help='FTP server host', required=True)
- args = parser.parse_args()
- duration = args['duration']
- interval = args['interval']
- image_format = args['image_format']
- ftp_user = args['ftp_user']
- ftp_password = args['ftp_password']
- ftp_host = args['ftp_host']
- ftp_client = get_ftp_client(user=ftp_user, password=ftp_password, host=ftp_host)
- snap_to_ftp(ftp_client, duration=duration, interval=interval, image_format=image_format)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement