remote unzip

morecowbell hoteljorba at gmail.com
Sun Nov 2 01:27:01 EST 2008


On Nov 1, 5:35 am, Stefan Behnel <stefan... at behnel.de> wrote:
> morecowbell wrote:
> > i got a bunch of zip files on a remote server and need to get to one
> > of the files, an xml file, in each of the archives. fairly simple to
> > do locally with zipfile but i don't seem to be able to combine that
> > local code with the paramiko sftp client. a rather simplified code
> > snippet to illustrate:
>
> > import os
> > import paramiko
> > import StringIO
> > import zipfile
>
> > remote_file = 'remote.zip'
> > ssh = paramiko.SSHClient()
> > ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh",
> > "known_hosts")))
> > ssh.connect('www.xxx.com', username='root', password='it_aint')
> > sftp = ssh.open_sftp()
> > dirlist = sftp.listdir('.')
> > zipfile = sftp.file('remote.zip', 'r')
> > unzip = ZipFile(zfile)
> > for f in unzip.namelist():
> >     if f.endswith('.xml'):
> >         output.write(unzip.read(unzip))
> >         xmlfiles.append(output.getvalue())
>
> > sftp.close()
> > ssh.close()
>
> I suppose this code doesn't work for you? What's the output you get?
>
> "don't seem to be able" is not a very helpful problem description.
>
> Stefan

thanks for looking at this. problem is, code seems to get lost in in
space.when the zipfile portion gets invoked:

run this:
import os
import paramiko
import StringIO
import zipfile

remote_file = 'remote.zip'
xml_files = []
ssh = paramiko.SSHClient()
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh",
"known_hosts")))
try:
    ssh.connect('www.xxx.com', username='root', password='itaint')
except Exception, e:
    print (e.__class__,e)
sftp = ssh.open_sftp()
dirlist = sftp.listdir('.')
print dirlist
#zfile = sftp.file(remote_file, 'r')
#print zfile
#unzip = ZipFile(zfile)
#for f in unzip.namelist():
#    if f.endswith('.xml'):
#        output.write(unzip.read(unzip))
#        xmlfiles.append(output.getvalue())
sftp.close()
ssh.close()

and we get the dirlist output:
PyMate r8111 running Python 2.5.1 (python)
>>> untitled

['.bash_profile', 'remote.zip', '.zshrc', '.mozilla', '.bash_logout',
'.emacs', '.bashrc']
Program exited.

uncomment the zfile line, i get the paramiko file object:
PyMate r8111 running Python 2.5.1 (python)
>>> untitled

['.bash_profile', 'odb012007101.zip', '.zshrc', '.mozilla',
'.bash_logout', '.emacs', '.bashrc']
<paramiko.SFTPFile object at 0x480770>
Program exited.

uncomment the next line, where the unzipping is done, and the program
goes off to digital neverland. i don't seem to be able to do anything
with the paramiko.STFPfile object. even print dir(zfile) ends up in
the script not completing.

thx






More information about the Python-list mailing list