Skip to main content

Thread: [Python] FTP script is hanging


i have written script in python connects ftp server , uploads data. i'm using daily automatic backups. problem if internet connection gets lost , connected again script hang. in case provider disconnects me after 24 hours , gives me new ip address. here script:

code:
#!/usr/bin/python -oott  # coding=utf-8  import stringio, argparse, ftplib, os, socket, sys, time    class backup:  	def __init__(self):  		arguments = argparse.argumentparser()  		arguments.add_argument('--serverdir', default = '.', dest = 'serverdir', = 'the directory on server backup stored')  		arguments.add_argument('-d', '--device', default = 'sr0', dest = 'device', = 'selects device data backuped')  		arguments.add_argument('-l', '--limit', type = int, dest = 'limit', = 'selects upload limit in bytes')  		arguments.add_argument('-p', '--password', required = true, dest = 'password', = 'the password ftp server')  		arguments.add_argument('-s', '--server', required = true, dest = 'server', = 'the address of ftp server')  		arguments.add_argument('-u', '--user', required = true, dest = 'user', = 'the username ftp server')  		option = arguments.parse_args()  		if option.limit != none , option.limit <= 0:  			print('the upload limit must 1 or higher')  			exit()  		if option.serverdir == '':  			print('the server directory invalid')  			exit()  		mount = true  		if os.system('sudo mount \'/dev/' + option.device + '\' \'/media/' + option.device + '\' -o ro -t iso9660 > /dev/null 2>&1') == 0:  			mount = false  		while 1:  			try:  				self.connection = ftplib.ftp(option.server, option.user, option.password)  				break  			except socket.error:  				time.sleep(1)  		serverdir = option.serverdir  		if serverdir[-1] == '/':  			serverdir = serverdir[:-1]  		i = 1  		while self.file_exists(serverdir + '_old-' + str(i)) == true:  			i += 1  		if self.file_exists(serverdir) == true:  			self.connection.rename(serverdir, serverdir + '_old-' + str(i))  			while self.file_exists(serverdir + '_old-' + str(i)) == false:  				time.sleep(1)  		self.makedirs(serverdir)  		for object in os.listdir('/media/' + option.device):  			file = open('/media/' + option.device + '/' + object)  			if option.limit != none:  				self.connection.storbinary('stor ' + serverdir + '/' + object, file, option.limit, self.control)  			else:  				self.connection.storbinary('stor ' + serverdir + '/' + object, file)  			file.close()  		if self.file_exists(serverdir + '_old-' + str(i)) == true:  			self.rmdir(serverdir + '_old-' + str(i))  		self.connection.quit()  		if mount == false:  			os.system('sudo umount \'/dev/' + option.device + '\'')    	def control(self, chunk):  		time.sleep(1)    	def file_exists(self, path):  		directory = path.split('/')  		last_directory = ''  		for object_1 in directory:  			if object_1 == '':  				continue  			for object_2 in self.get_files(last_directory):  				if object_1 == object_2:  					last_directory += object_1 + '/'  					break  			if object_1 != object_2:  				return false  		return true    	def get_files(self, path):  		stdout = stringio.stringio()  		sys.stdout = stdout  		self.connection.dir(path)  		list = stdout.getvalue()[:-1]  		stdout.close()  		sys.stdout = sys.__stdout__  		file = []  		for entry in list.split('\n'):  			file.append(entry.split()[-1])  		return file    	def makedirs(self, path):  		directory = path.split('/')  		last_directory = ''  		for object in directory:  			if object == '':  				continue  			if self.file_exists(last_directory + object) == false:  				self.connection.mkd(last_directory + object)  				last_directory += object + '/'    	def rmdir(self, path):  		for object in self.connection.nlst(path):  			if object == '.' or object == '..':  				continue  			try:  				self.connection.size(path + '/' + object)  				self.connection.delete(path + '/' + object)  			except:  				self.rmdir(path + '/' + object)  		self.connection.rmd(path)    backup()
it seems self.connection.storbinary causing problem. doesn't send data or raising exception. use threads , check if transfer aborted. way must resend complete data every time if connection gets lost. maybe know how continue transfer if connection gets lost.

i have opened ticket @ python bugtracker this: http://bugs.python.org/issue13714

problem appears if ip address changes during connection. python can't detect change , correcting ftp connection.

have found solution. i'm using timeout argument , counting how many data have send. after disconnect i'm opening new connection , sending rest of data (this looped because have wait until first connection got timeout server because blocks file).


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] [Python] FTP script is hanging


Ubuntu

Comments

Popular posts from this blog

How to set the order of FAQs instead of alphabetical

Thread: Get UK Keyboard working

how do I change the e-mail address for my merchant account