pdf SFTP
Par keyphrene, Saturday 27 September 2008 à 13:57 :: Org.keyphrene - 4Py :: #7 :: rss
How to open a sftp connection and list a directory
How to open a sftp connection and list a directory
import socket from org.keyphrene import SSH2 # or # from ssh4py import SSH2 HOSTNAME = "127.0.0.1" PORT = 22 LOGIN = "login" PWD = "password" PATH = "/home/login" sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((HOSTNAME, PORT)) sock.setblocking(1) ssh = SSH2.Session() ssh.setBanner(SSH2.DEFAULT_BANNER+" Python (http://www.keyphrene.com/projects/org.keyphrene)") ssh.startup(sock) ssh.setPassword(LOGIN, PWD) # open a SFTP channel sftp = ssh.SFTP() # get filesize stats = sftp.getStat("/path/to/filename.txt") filesize=stats[0] # Filesize uid = stats[1] # UID gid = stats[2] # GID permissions = stats[3] # Permissions ex: rw-rw-rw atime = stats[4] # Time of most recent access mtime = stats[5] # Time of most recent content modification handle = sftp.openDir(PATH) if handle: # Open a sftp connection and list a directory while 1: data = sftp.readDir(handle) if not data: break print data # Open a sftp connection and list a directory with attributes for f, attr in sftp.listDir(handle): print f, attr # Open a file channel = sftp.open("/path/to/filename.txt", "rb") while 1: data = sftp.read(channel, 1024) if not data: break print data sftp.close(channel) # Save a file channel = sftp.open("/path/to/filename.txt", "wb") sftp.write(channel, "my data ...") sftp.close(channel) # close the SFTP channel sftp.close(handle) sftp.shutdown() ssh.close()

Comments
No comment for this moment.
Add a comment