enn ich den nachfolgenden, ausführbaren Quelltext ausführe, dann ende ich zum Schluss bei der Fehlermeldung ValueError: Value out of range (Am Ende dieses Beitrags siehst du den vollen Traceback). Die Datei wird wie gewöhnlich und erfolgreich zum FTP-Server hochgeladen. Jedoch bricht die Progressbar bei 100 Prozent ab Sie kommt nur bis 99 % und dann folgt die Meldung. Hat jemand eine Ahnung, woran es liegen könnte?
Code: Alles auswählen
from ftplib import FTP_TLS
from os import path
from sys import exc_info
import socket
from progressbar import ProgressBar, Bar, Percentage
from traceback import format_exc
class FTP_Client(object):
def __init__(self,
ftp_url=None,
ftp_port=None,
ftp_user_name=None,
ftp_user_pwd=None,
ftp_operation=None,
file_name=None):
self.uploaded_byte= 0
self.totalSize = 0
self.lastShownPercent = 0
self.pbar = None
self.ftp_url = ftp_url
self.ftp_port = ftp_port
self.ftp_operation = ftp_operation
self.ftp_user_name = ftp_user_name
self.ftp_user_pwd = ftp_user_pwd
self.file_name= file_name
self.totalSize = self.get_total_file_size(file_path=self.file_name)
'''
# Open the file for reading in binary mode
'''
self.opened_file = open(self.file_name, 'rb')
self.open_ftp_connect(url_ftp=self.ftp_url,
port_ftp=self.ftp_port)
def get_total_file_size(self, file_path=None):
return float(path.getsize(file_name))
def open_ftp_connect(self,
url_ftp=None,
port_ftp=None):
# Open FTP connection, and set the session as the variable client
client = FTP_TLS(timeout=100)
client.connect(url_ftp, port_ftp)
# enable TLS
client.auth()
client.prot_p() # switch to secure data connection
# securing data connection explicitly
self.login_ftp(ftp_connect_obj=client,
ftp_user_name=self.ftp_user_name,
ftp_user_password=self.ftp_user_pwd)
def login_ftp(self,
ftp_connect_obj=None,
ftp_user_name=None,
ftp_user_password=None):
ftp_connect_obj.login(ftp_user_name,ftp_user_password)
print "TEXT", ftp_connect_obj.getwelcome()
print "PWD", ftp_connect_obj.sendcmd('PWD')
self.change_directiory(ftp_connect_obj=ftp_connect_obj,
directory='/python')
def change_directiory(self,
ftp_connect_obj=None,
directory=None):
ftp_connect_obj.cwd(directory) # change working directory to the root
try:
ftp_operation_func_dict = {'d': self.download_file,
'u': self.upload_file}
ftp_operation_func_dict[self.ftp_operation](ftp_connect_obj=ftp_connect_obj)
print "File transfered"
except KeyError:
print "The function doesn't exist! Try again!"
except Exception as a_err:
print "Connection lost. Error in File transfering: ", a_err
desired_trace = format_exc(exc_info())
print "desired_trace", desired_trace
def download_file(self,
ftp_connect_obj=None):
pass
def upload_file(self,
ftp_connect_obj=None):
'''
NOTICE:
=======
Here we upload the given file a chunk at a time
Each chunk is sent to handle_progress
We append the chunk to the file.
STOR is an FTP command
'''
file_size = self.totalSize
result = file_size / (1024*5)
chunk_size = int(result)
uploaded_bytes = 0
destination = 'tester.rar'
file_handle = 0
if self.pbar is None:
self.pbar = ProgressBar(maxval=self.totalSize)
self.pbar.start()
## Open a file to upload
with open(self.file_name, 'rb') as fd:
file_handle = fd
ftp_connect_obj.storbinary('STOR %s' % destination, file_handle, blocksize=chunk_size, callback=lambda conn_obj=ftp_connect_obj:
self.handle_update_file_transfer_progress(chunk=chunk_size))
self.pbar.finish()
self.pbar = None
self.close_quite_ftp_connect(ftp_connect_obj=ftp_connect_obj)
def handle_update_file_transfer_progress(self, chunk=None):
'''
NOTICE:
======
This method is defined to show the percentage uploaded
'''
self.uploaded_byte += chunk
self.pbar.update(self.uploaded_byte)
def close_quite_ftp_connect(self, ftp_connect_obj=None):
# Close the connection and the file
print ftp_connect_obj.quit()
print ftp_connect_obj.close()
if __name__ == "__main__":
ftp_url = raw_input('Enter URL of FTP without // at the begin: ')
ftp_port = raw_input('Enter FTP-Port: ')
ftp_user_name = raw_input('Enter Username: ')
ftp_user_pwd = raw_input('Enter Password: ')
choice = raw_input("(U)pload or (D)ownload?: ")
file_name = raw_input('Enter file path with file name: ')
try:
uploadTracker = FTP_Client(ftp_url=ftp_url,
ftp_port=ftp_port,
ftp_user_name=ftp_user_name,
ftp_user_pwd=ftp_user_pwd,
ftp_operation=choice.lower(),
file_name=file_name)
except socket.gaierror:
# When your computer can't resolve domain name, you're gave to him.
print "Can't open the connection"
except WindowsError:
print "ERROR: There isn't a file to open!"
Connection lost. Error in File transfering: Value out of range
desired_trace Traceback (most recent call last):
File "C:\Users\Sophus\Desktop\py_scripts\ftp_secure.py", line 92, in change_directiory
ftp_operation_func_dict[self.ftp_operation](ftp_connect_obj=ftp_connect_obj)
File "C:\Users\Sophus\Desktop\py_scripts\ftp_secure.py", line 157, in upload_file
ftp_connect_obj.storbinary('STOR %s' % destination, file_handle, blocksize=chunk_size, callback=lambda conn_obj=ftp_connect_obj:
File "C:\Python27\lib\ftplib.py", line 749, in storbinary
if callback: callback(buf)
File "C:\Users\Sophus\Desktop\py_scripts\ftp_secure.py", line 158, in <lambda>
self.handle_update_file_transfer_progress(chunk=chunk_size))
File "C:\Users\Sophus\Desktop\py_scripts\ftp_secure.py", line 175, in handle_update_file_transfer_progress
self.pbar.update(self.uploaded_byte)
File "build\bdist.win32\egg\progressbar\__init__.py", line 271, in update
raise ValueError('Value out of range')
ValueError: Value out of range