Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- encoding:utf-8 -*-
- ''' Send files through FTPS '''
- # FTP Defaults
- FTP_ADDRESS = '127.0.0.1'
- FTP_PORT = 21
- FTP_USER = 'ftp'
- FTP_PASSWORD = ''
- # Folder name format
- # %a Locales abbreviated weekday name.
- # %A Locales full weekday name.
- # %b Locales abbreviated month name.
- # %B Locales full month name.
- # %c Locales appropriate date and time representation.
- # %d Day of the month as a decimal number [01,31].
- # %H Hour (24-hour clock) as a decimal number [00,23].
- # %I Hour (12-hour clock) as a decimal number [01,12].
- # %j Day of the year as a decimal number [001,366].
- # %m Month as a decimal number [01,12].
- # %M Minute as a decimal number [00,59].
- # %p Locales equivalent of either AM or PM.
- # %S Second as a decimal number [00,61].
- # %U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53].
- # All days in a new year preceding the first Sunday are considered to be in week 0.
- # %w Weekday as a decimal number [0(Sunday),6].
- # %W Week number of the year (Monday as the first day of the week) as a decimal number [00,53].
- # All days in a new year preceding the first Monday are considered to be in week 0.
- # %x Locales appropriate date representation.
- # %X Locales appropriate time representation.
- # %y Year without century as a decimal number [00,99].
- # %Y Year with century as a decimal number.
- # %Z Time zone name (no characters if no time zone exists).
- # %% A literal '%' character.
- FOLDER_NAME_FORMAT = '%d.%m.%Y'
- import sys, os, time, ftplib#, socket
- # M2Crypto required
- try:
- import M2Crypto
- except:
- print 'Error: M2Crypto is not found. Please install it and rerun script.'
- sys.exit(1)
- log = open('ftps_send.log', 'w')
- def connect_ftps(address, port, login, password):
- ''' Connect to FTPS '''
- #socket.setdefaulttimeout(50.0)
- conn = M2Crypto.ftpslib.FTP_TLS()
- # conn = ftplib.FTP()
- conn.connect(address, port)
- conn.auth_tls()
- conn.set_pasv(0)
- conn.login(login, password)
- conn.prot_p()
- # conn.voidcmd('TYPE I')
- return conn
- def send_file(conn, file_name):
- ''' Send file through connection.
- Very slow due to bug in ftplib.
- '''
- global log
- log.write('send_file(' + file_name + ')\n')
- f = open(file_name, 'rb')
- try:
- conn.storbinary('STOR ' + os.path.basename(file_name), f)
- except ftplib.error_reply, e:
- log.write(str(e) + '\n')
- except ftplib.all_errors, e:
- log.write(str(e) + '\n')
- code = int(str(e).split()[0])
- if code not in [200, 250, 451, 550]:
- raise e # everything else is very bad
- f.close()
- def send_file2(conn, file_name):
- ''' Send file through connection.
- Transfer delay bug workaround.
- '''
- global log
- log.write('send_file2(' + file_name + ')\n')
- try:
- conn.voidcmd('TYPE I')
- #conn.set_pasv(True)
- sock = conn.transfercmd('STOR ' + os.path.basename(file_name)) # the data socket
- except ftplib.error_reply, e:
- log.write(str(e) + '\n')
- except ftplib.all_errors, e:
- log.write(str(e) + '\n')
- code = int(str(e).split()[0])
- if code not in [200, 250, 451, 550]:
- raise e # everything else is very bad
- f = open(file_name, 'rb')
- sock.send(f.read())
- sock.close()
- f.close()
- def create_dir(conn, dir_name, cd=False):
- ''' Creates directory and optionally changes into it.
- '''
- log.write('create_dir(' + dir_name + ', ' + str(cd) + ')\n')
- try:
- conn.mkd(dir_name)
- #conn.voidcmd('SITE CHMOD 0751 ' + dir_name)
- except ftplib.error_reply, e:
- log.write(str(e) + '\n')
- except ftplib.all_errors, e:
- log.write(str(e) + '\n')
- code = int(str(e).split()[0])
- if code not in [200, 250, 451, 550]: # it is ok for dir to exists
- raise e # everything else is very bad
- if cd:
- log.write('cd ' + dir_name + '\n')
- try:
- conn.cwd(dir_name)
- except ftplib.error_reply, e:
- log.write(str(e) + '\n')
- except ftplib.all_errors, e:
- log.write(str(e) + '\n')
- code = int(str(e).split()[0])
- if code not in [200, 250, 451, 550]:
- raise e # everything else is very bad
- def process_dir(conn, directory):
- ''' Process local directory.
- '''
- log.write('process_dir()\n')
- for subdir, dirs, files in os.walk(directory):
- for sdir in dirs:
- #print 'dir', directory, sdir
- create_dir(conn, sdir, True)
- process_dir(conn, os.path.join(directory, sdir))
- for i in range(len(dirs)): del dirs[0]
- for file_name in files:
- #print 'file', directory, file_name
- send_file2(conn, os.path.join(directory, file_name))
- log.write('cd ..\n')
- conn.cwd('..')
- def usage():
- print 'Usage:', sys.argv[0],'[OPTION]...'
- print 'Uploads file from local directory to FTPS server.'
- print 'You should specify at least --local-dir.'
- print ''
- print '--host\t\t\tFTPS server address'
- print '--port\t\t\tFTPS server port'
- print '--user\t\t\tuser name to log in with'
- print '--password\t\tuser password to log in with'
- print '--local-dir\t\tlocal directory with files to upload'
- print '--remote-dir\t\tbase directory on remote server'
- print '--folder-format\t\tcreate folder on server with format'
- def main():
- ''' Main function
- '''
- # Process arguments
- ftp_address = FTP_ADDRESS
- ftp_port = FTP_PORT
- ftp_user = FTP_USER
- ftp_password = FTP_PASSWORD
- local_dir = None
- remote_dir = '/'
- folder_name_format = FOLDER_NAME_FORMAT
- #
- for i in range(len(sys.argv)):
- if sys.argv[i] == '--host':
- if len(sys.argv) < i + 2:
- usage()
- sys.exit(1)
- ftp_address = sys.argv[i + 1]
- if sys.argv[i] == '--port':
- if len(sys.argv) < i + 2:
- usage()
- sys.exit(1)
- ftp_port = int(sys.argv[i + 1])
- if sys.argv[i] == '--user':
- if len(sys.argv) < i + 2:
- usage()
- sys.exit(1)
- ftp_user = sys.argv[i + 1]
- if sys.argv[i] == '--password':
- if len(sys.argv) < i + 2:
- usage()
- sys.exit(1)
- ftp_password = sys.argv[i + 1]
- if sys.argv[i] == '--local-dir':
- if len(sys.argv) < i + 2:
- usage()
- sys.exit(1)
- local_dir = sys.argv[i + 1]
- if sys.argv[i] == '--remote-dir':
- if len(sys.argv) < i + 2:
- usage()
- sys.exit(1)
- remote_dir = sys.argv[i + 1]
- if sys.argv[i] == '--folder-format':
- if len(sys.argv) < i + 2:
- usage()
- sys.exit(1)
- folder_name_format = sys.argv[i + 1]
- if local_dir is None:
- usage()
- sys.exit(1)
- conn = connect_ftps(ftp_address, ftp_port, ftp_user, ftp_password)
- conn.cwd(remote_dir)
- dir_name = time.strftime(folder_name_format, time.localtime())
- create_dir(conn, dir_name, True)
- #send_file2(conn, 'testfile')
- print 'Uploading...'
- process_dir(conn, local_dir)
- conn.quit()
- main()
- log.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement