remote unzip

M

morecowbell

greetings,


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()

any help is greatly appreacited
 
S

Stefan Behnel

morecowbell said:
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
 
M

morecowbell

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)
['.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)
['.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,828
Messages
2,569,736
Members
45,520
Latest member
JudithMorf

Latest Threads

Top