import ftplib
import string, socket
import time, ntptime
import config
import readsetting
readsetting = readsetting.READSETTING()
CONFIGFILE = "config.cfg"
class LFTP:
def __init__(self):
self.file_size = 0
self.status = 0
self.ftp = ftplib.FTP()
self.servername = config.ftpserver
self.userid = readsetting.get_user_id("/usr/local/lgvp/vdci.cfg")
self.userpw = ""
self.mode = 0 # 0:text mode, 1:binary mode
self.totalreceive = 0
self.conntime = ntptime.ntime()
self.filelist = []
config.firmware_file_response_code = []
config.firmware_update_response_phrase = []
config.firmware_file_response_crc = []
#by elpis, add return value
def connection(self, ServerName=None, UserID=None, UserPW=None):
while 1:
try:
if ServerName == None:
break
if config.update_debug:print "Connecting..."
self.ftp.connect(ServerName)
if config.update_debug:print "Connected to %s" % ServerName
except socket.gaierror:
if config.update_debug:print "No address associated with Server Name"
ServerName = None
return 0
else:
break
if UserID == None:
UserID = "ftp"
UserPW = "temp@temp.com"
while 1:
try:
test = self.ftp.login(UserID, UserPW)
if config.update_debug:print "User login Success"
except ftplib.error_perm:
self.ftp.close()
if config.update_debug:print "User login Fail"
return 0
break
else:
break
if config.update_debug:print "User Login Success!!!"
self.servername = ServerName
self.userid = UserID
self.userpw = UserPW
return 1
def getfilelist(self):
try :
conn = self.ftp.transfercmd('LIST')
fd = conn.makefile('rb')
while 1:
line = fd.readline()
if config.update_debug:print line
if len(line) == 0:
if config.update_debug:print "+ No such file : %s" % (filename)
break
if line[-2:] == "\r\n":
tmp = line[:-2]
elif line[-1:] == "\n":
tmp = line[:-1]
tmp2 = string.split(tmp)
self.filelist.append(tmp2)
conn.close()
fd.close()
except:
pass
def getfilesize(self, filename):
self.file_size = 0
for fname in self.filelist:
if config.update_debug:print fname
if fname[3] == filename:
self.file_size = int(fname[2]) #서버에 따라서 틀림
if config.update_debug:print 'self filesize :', self.file_size
break
if self.file_size != 0: break
def setremotepassive(self):
resp = self.ftp.sendcmd("PASV")
if config.update_debug:print resp
def setlocalpassive(self, mode): # need not to use, default is passive
if self.localpassive:
if mode:
if config.update_debug:print "Already local passive mode enabled!!!"
else:
self.ftp.set_pasv(mode)
self.localpassive = mode
else:
if mode:
self.ftp.set_pasv(mode)
self.localpassive = mode
else:
if config.update_debug:print "Already local passive mode disabled!!!"
def settransfermode(self, mode):
resp = self.ftp.sendcmd("TYPE I")
self.mode = 1
if config.update_debug:print resp
#changed
def parse_response_message(self, response):
config.info_response_code = int(response[:3])
# config.info_response_phrase = "\"" + response[4:] + "\"" //changed by elpis 20061115
config.info_response_phrase = "\'" + response[4:] + "\'"
config.firmware_update_response_code = config.info_response_code
config.config_response_code = config.info_response_code
config.config_response_phrase = config.info_response_phrase
config.firmware_file_response_code.append(str(config.info_response_code))
config.firmware_update_response_phrase.append(str(config.info_response_phrase))
if config.update_debug:print config.firmware_file_response_code
if config.update_debug:print config.firmware_update_response_phrase
def getrequest(self, downfile, writefile):
#insert try and except, and return value to process ftp error message by elpis
try:
#download = 'RETR ' + config.hardware_version + '/' + config.firmware_version + '/' + writefile
if self.mode:
if config.update_debug:print "downloading %s .... " % writefile
flen = 0
f = file(writefile, 'wb')
conn = self.ftp.transfercmd('RETR ' + downfile)
while 1:
block = conn.recv(8012)
if len(block) == 0:
break
flen = flen + len(block)
self.totalreceive += len(block)
f.write(block)
f.close()
conn.close()
return self.ftp.voidresp()
else:
self.getfilesize(filename)
if config.update_debug:print "get text mode"
self.ftp.retrlines('RETR ' + downfile, open(writefile, 'wb').write)
return self.ftp.voidresp()
except:
import sys
type, value, tb = sys.exc_info()
return str(value)
#return self.ftp.response
def getfile(self, downfile, writefile, comm):
#insert try and except, and return value to process ftp error message by elpis
try:
#download = 'RETR ' + config.hardware_version + '/' + config.firmware_version + '/' + writefile
if self.mode:
if config.update_debug:print "downloading %s .... " % writefile
flen = 0
f = file(writefile, 'wb')
conn = self.ftp.transfercmd('RETR ' + downfile)
while 1:
block = conn.recv(8012)
if len(block) == 0:
break
flen = flen + len(block)
self.totalreceive += len(block)
f.write(block)
comm.send_packet("total_received_files_size")
comm.send_packet(str(self.totalreceive))
time.sleep(0)
f.close()
conn.close()
return self.ftp.voidresp()
else:
self.getfilesize(filename)
if config.update_debug:print "get text mode"
self.ftp.retrlines('RETR ' + downfile, open(writefile, 'wb').write)
return self.ftp.voidresp()
except:
import sys
type, value, tb = sys.exc_info()
return str(value)
#return self.ftp.response
def reconnect(self,tar = None):
try:
self.ftp.quit()
except:
pass
if config.update_debug:print "+ starting reconnection."
self.connection(self.servername ,self.userid , self.userpw)
def putfile(self, upfile):
try:
file = "STOR " + upfile
if config.update_debug:print file
resp = self.ftp.storbinary(file, open(upfile, 'r'))
if config.update_debug:print resp
except:
if config.update_debug:print "Can't upload : %s" % upfile
pass