views:

25

answers:

0

I'm using paramiko to transfer files with SFTP, on a apache server with mod_python.

It works fine if I open the SFTP connection in the function called by mod_python (ex. index(req) ).

But when I have 10-15 files that all uses a SFTP connection, I want to use a module that starts this connection.

I have tried to make a function that returns the sftp class, and making the sftp variable a global variable, both with the same result.

If you look at the error below the paramiko class is in it self working.

Do anyone know how to make this work? or another (better) way to do this?

Edit: I found a known bug in paramiko that describes the same error (http://bugs.launchpad.net/paramiko/+bug/567330), but not the same cause, since I don't open the connection more then once.

The code for the first one:

--- common.py ---

def getSFTP():
    ssh = paramiko.SSHClient()  
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  
    ssh.connect(SSH_HOST, username=SESSION['usr'], password=SESSION['pwd'])  
    sftp = ssh.open_sftp()  
    sftp.chdir(SESSION['cwd'])
    return sftp

--- index.py ---
import common  

def index(req):
    sftp = common.getSFTP()  
    l = sftp.listdir(".")

The code for the second one:

--- common.py ---  
SFTP

def startSFTP():
    global SFTP

    ssh = paramiko.SSHClient()  
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  
    ssh.connect(SSH_HOST, username=SESSION['usr'], password=SESSION['pwd'])  
    sftp = ssh.open_sftp()  
    sftp.chdir(SESSION['cwd'])

    return True

--- index.py ---  
import common  

def index(req):
    common.startSFTP()

    l = common.SFTP.listdir(".")

The error I get is this:

  File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1537, in HandlerDispatch
    default=default_handler, arg=req, silent=hlist.silent)

  File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1229, in _process_target
    result = _execute_target(config, req, object, arg)

  File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1128, in _execute_target
    result = object(arg)

  File "/usr/lib/python2.6/dist-packages/mod_python/publisher.py", line 213, in handler
    published = publish_object(req, object)

  File "/usr/lib/python2.6/dist-packages/mod_python/publisher.py", line 425, in publish_object
    return publish_object(req,util.apply_fs_data(object, req.form, req=req))

  File "/usr/lib/python2.6/dist-packages/mod_python/util.py", line 554, in apply_fs_data
    return object(**args)

  File "/var/www/index.py", line 17, in index
    sftpfolders = folder.listfolder(common.getSftp(), dir)

  File "/var/www/common.py", line 158, in listfolder
    l = sftp.listdir(dir)

  File "/usr/lib/pymodules/python2.6/paramiko/sftp_client.py", line 150, in listdir
    return [f.filename for f in self.listdir_attr(path)]

  File "/usr/lib/pymodules/python2.6/paramiko/sftp_client.py", line 173, in listdir_attr
    t, msg = self._request(CMD_OPENDIR, path)

  File "/usr/lib/pymodules/python2.6/paramiko/sftp_client.py", line 627, in _request
    num = self._async_request(type(None), t, *arg)

  File "/usr/lib/pymodules/python2.6/paramiko/sftp_client.py", line 649, in _async_request
    self._send_packet(t, str(msg))

  File "/usr/lib/pymodules/python2.6/paramiko/sftp.py", line 172, in _send_packet
    self._write_all(out)

  File "/usr/lib/pymodules/python2.6/paramiko/sftp.py", line 138, in _write_all
    raise EOFError()

EOFError