Code: Alles auswählen
>>> ftp.sendcmd('MDTM ' + 'test.dat')
'213 20100119140243'
>>>
Danke.
Code: Alles auswählen
>>> ftp.sendcmd('MDTM ' + 'test.dat')
'213 20100119140243'
>>>
Code: Alles auswählen
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)
strptime=timestruct
atime= time.mktime(strptime)
pfad=os.path.join(dest, filename)
print pfad
times = (atime,atime)
print times
os.utime(pfad,times)
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)
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'
Code: Alles auswählen
help(timeftpx.get_last_modified)
Code: Alles auswählen
help(timeftpx.get_last_modified
Code: Alles auswählen
def get_last_modified
Code: Alles auswählen
import timeftpx
File "C:\Python27\lib\timeftpx.py", line 24
def get_last_modified(ftp_addr, pattern='*', no=1, dest='.', user='', pw=''):
^
SyntaxError: invalid syntax
Code: Alles auswählen
Help on function get_last_modified in module timeftpx:
get_last_modified(ftp_addr, pattern='*', no=1, dest='.', user='', pw='')
Code: Alles auswählen
Help on class Connection in module pysftp:
class Connection(__builtin__.object)
| Connects and logs into the specified hostname.
| Arguments that are not given are guessed from the environment.
| host - The Hostname of the remote machine.
| username - Your username at the remote machine.(None)
| private_key - Your private key file.(None)
| password - Your password at the remote machine.(None)
| port - The SSH port of the remote machine.(22)
| private_key_pass - password to use if your private_key is encrypted(None)
| log - log connection/handshake details (False)
| returns a connection to the requested machine
|
| srv = pysftp.Connection('example.com')
|
| Methods defined here:
|
| __del__(self)
| Attempt to clean up if not explicitly closed.
|
| __init__(self, host, username=None, private_key=None, password=None, port=22, private_key_pass=None, log=False)
|
| chdir(self, path)
| change the current working directory on the remote
|
| close(self)
| Closes the connection and cleans up.
|
| execute(self, command)
| Execute the given commands on a remote machine.
|
| get(self, remotepath, localpath=None)
| Copies a file between the remote host and the local host.
|
| getcwd(self)
| return the current working directory on the remote
|
| listdir(self, path='.')
| return a list of files for the given path
|
| put(self, localpath, remotepath=None)
| Copies a file between the local host and the remote host.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
Code: Alles auswählen
FTP = TimeFTP(parsed.netloc, username, password)
Code: Alles auswählen
Traceback (most recent call last):
File "C:\PASU_FTP_Export\Sourcecode\testmini", line 7, in <module>
timeftpx.get_last_modified('host', '*.dat', 2, 'C:\user', 'username','password')
File "C:\Python27\lib\timeftpx.py", line 29, in get_last_modified
FTP = TimeFTP(parsed.netloc, username, password)
File "build\bdist.win32\egg\pysftp.py", line 55, in __init__
self._transport = paramiko.Transport((host, port))
File "build\bdist.win32\egg\paramiko\transport.py", line 298, in __init__
'Unable to connect to %s: %s' % (hostname, reason))
SSHException: Unable to connect to : [Errno 10049] Die angeforderte Adresse ist in diesem Kontext ungültig
Code: Alles auswählen
In [2]: urlparse.urlparse('host').netloc
Out[2]: ''
Code: Alles auswählen
import pysftp
srv = pysftp.Connection(host='testserver',username='usr', password='test')
srv.execute('ls -al')
srv.chdir('/dat3')
srv.get('hb.txt')
#srv.put('hb.txt')
srv.close()