nach langer Zeit habe ich die Aufgabe bekommen mein FTP-Programm auf SFTP umzustellen.
Hierzu habe ich den Code mal teilweise angepasst:
Code: Alles auswählen
import pysftp
from operator import itemgetter
import os.path
import time
import urlparse
class TimeFTP(pysftp.Connection):
def mdtm(self, filename):
retcode, timestamp = self.sendcmd('MDTM ' + filename).split()
return timestamp
def last_modified(self, pattern):
results = []
for filename in self.nlst(pattern):
timestamp = self.mdtm(filename)
results.append((filename, timestamp))
for filename, timestamp in sorted(results, key=itemgetter(1)):
timestruct = time.strptime(timestamp, '%Y%m%d%H%M%S')
yield filename, timestruct
def get_last_modified(ftp_addr, pattern='*', no=1, dest='.', user='', pw=''):
parsed = urlparse.urlparse(ftp_addr)
FTP = TimeFTP(parsed.netloc, user, pw)
if parsed.path:
ftp.cwd(parsed.path)
for filename, timestruct in list(ftp.last_modified(pattern))[-no:]:
with open(os.path.join(dest, filename), 'wb') as outfile:
ftp.retrbinary('RETR ' + filename, outfile.write)
Was mir jetzt nicht klar ist,was will mir die Fehlermeldung sagen:
Code: Alles auswählen
Traceback (most recent call last):
File "C:/Python_Test/Sourcecode/testmini", line 4, in <module>
timeftpx.get_last_modified(ftp_addr, '*.dat', 2, 'C:\user', 'login','pass')
File "C:\Python27\lib\timeftpx.py", line 25, in get_last_modified
FTP = TimeFTP(parsed.netloc, user, pw)
File "build\bdist.win32\egg\pysftp.py", line 74, in __init__
xSx_key = paramiko.RSAKey.from_private_key_file(private_key_file,private_key_pass)
File "build\bdist.win32\egg\paramiko\pkey.py", line 198, in from_private_key_file
key = cls(filename=filename, password=password)
File "build\bdist.win32\egg\paramiko\rsakey.py", line 51, in __init__
self._from_private_key_file(filename, password)
File "build\bdist.win32\egg\paramiko\rsakey.py", line 163, in _from_private_key_file
data = self._read_private_key_file('RSA', filename, password)
File "build\bdist.win32\egg\paramiko\pkey.py", line 279, in _read_private_key_file
f = open(filename, 'r')
IOError: [Errno 2] No such file or directory: 'pass'