[mod_python] Paramiko connection to SFTP web-based client

Graham Dumpleton graham.dumpleton at gmail.com
Thu Mar 11 19:32:31 EST 2010


On 12 March 2010 09:09, Mark Brown <Mark.Brown at greenskycredit.com> wrote:
> Sorry that I am really, really new to Python but I have run into a wall and
> need some help:
>
> Basically, I am trying to create a web-client for a sftp server.
>
>
>
> You enter your user name and password and you authenticate against the sftp
> server, see a list of all the files on the server, can browse the server,
> and if you click on a link download the file.
>
> I have tried several different methods to keep the network-resource in the
> session, but nothing thus far has worked. My research seems to indicate that
> in order to achieve this I would need to write a daemon that handles the
> connection, and pass the session id to the daemon to match connection to web
> request.
>
>
>
> I have found some articles on how to turn a python script into a daemon, but
> I need some help understanding what is really going.
>
> Anyways, the script  I have tested thus far using mod_python is below:
>
>
>
> from mod_python import apache
> from mod_python import Session
> import paramiko
>
>
>
> def handler(req):
>       session=Session.Session(req)
>       req.content_type="text/html"
>
>       try:
>             session['hits'] +=1
>             #dirlist=session['sftp'].listdir('.')
>             req.write('From Session')
>       except:
>             t=paramiko.Transport(('[SERVER NAME],22))
>             t.connect(username='[USER NAME]',password='[PASSWORD]')
>             sftp=paramiko.SFTPClient.from_transport(t)
>             dirlist = sftp.listdir('.')
>             session['hits']=1
>             session['sftp']=sftp
>             req.write('New Session')
>
>       session.save()
>       req.write('Hits %d\n' % session['hits'])
>       req.write(dirlist)
>
>       return apache.OK

To ensure portability and overall guarantee it will always work, you
should only store in session basic Python data types. Even class
objects should be avoided even if they can be pickled. This is because
normally the session is stored in a database outside of the process.
Complex objects such as a socket handle obviously cant therefore be
stored.

The only time doing so would work is on Windows. That is because in
that case the session database is in memory and isn't actually written
to a separate database. You could only make it work on UNIX systems by
changing your Apache configuration such that it only ever used a
single server child process such that all requests handled by same
process and forcing the use of in memory session database. Forcing a
single process when using mod_python on UNIX sort of defeats the
problem of using Apache.

You could use this single process model by instead using mod_wsgi as
at least with that you could use its daemon mode and force your web
application to run in single dedicated process separate to the Apache
server child processes. You then though have to learn WSGI or use one
of the WSGI frameworks/toolkits. You still couldn't use traditional
session layers though and would need to maintain your own data
structure with active pool of connections in memory.

BTW, are you sure there aren't already web interfaces to SFTP clients?
Sure there would be one somewhere, albeit maybe implemented in a
different language.

Graham



More information about the Mod_python mailing list