So, I've developed a small Python script to automate this task and avoid the timeouts.
It has the following features:
- can be scheduled;
- accepts file masks;
- assumes a default local directory where the files to be uploaded are located;
- supports text files only;
- opens the connection for each file transfer in order to avoid timeout problems;
# -*- coding: iso-8859-15 -*-
'''
Automatic FTP
'''
import sys
import string
import ftplib
import os
import glob
import datetime
# Server name or IP address
__SERVER = "server.com"
# FTP username
__USER = "username"
# FTP password
__PASSWORD = "password"
# Default local directory were files are located to be uploaded
__DEFAULT_LOCATION = "C:\\ftp\\upload"
#
# Shows the time difference.
#
def showTimeDifference(init):
td = datetime.datetime.now()-init
print "in",td.seconds,"seconds (",td.seconds/60," minutes )."
#
# Transfers the files
#
def transferFile(ftpFile):
ftpInit = datetime.datetime.now()
file = os.path.basename(ftpFile)
print "\tTransfering:",file,
ftp = ftplib.FTP(__SERVER)
ftp.login(__USER, __PASSWORD)
ftp.storlines("STOR " + str(file).upper(), open(ftpFile))
ftp.close()
showTimeDifference(ftpInit)
print ""
#
# Get the files to transfer
#
def getTransferFiles(location, mask):
result = []
for file in glob.glob1(location, mask):
result.append(os.path.join(location, file))
return result
#
# Main
#
def main(args):
fileMask = ""
fileLocation = __DEFAULT_LOCATION
if len(args) == 2:
fileMask = sys.argv[1]
elif len(args) == 3:
fileMask = args[1]
fileLocation = args[2]
else:
print "autoFTP filemask [location]"
sys.exit(0)
print "Transfer files from",fileLocation,"to", __SERVER,"..."
ftpInit = datetime.datetime.now()
for file in getTransferFiles(fileLocation, fileMask):
transferFile(file)
print "Transfer competed",
showTimeDifference(ftpInit)
#
# Main
#
main(sys.argv)
After setting things up for one's needs, it's quite easy to use.
To transfer all .txt files starting with "A" from the default local directory, just:
python autoFTP A*.txt
To transfer all .xml files from a specific directory "Z:\test", just:
python autoFTP *.xml Z:\test
If one needs to adapt this to either get file, i.e. download, or to support binary file, just check the Python ftplib documentation, it's quite easy.
./M6