Paramiko connection to SFTP web-based client

Mark Brown <[email protected]> Thu, 11 Mar 2010 17:09:05 -0500
Newsgroups gmane.comp.python.mod_python
Message-ID <0B1220B1BC53C04384C8EACA4EEA24C2814EE259A6@GS-EXCH-01.greensky.local>
--===============0712758592==
Content-Language: en-US
Content-Type: multipart/alternative;
	boundary="_000_0B1220B1BC53C04384C8EACA4EEA24C2814EE259A6GSEXCH01green_"

--_000_0B1220B1BC53C04384C8EACA4EEA24C2814EE259A6GSEXCH01green_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

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 tha=
t 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 we=
b request.

I have found some articles on how to turn a python script into a daemon, bu=
t 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=3DSession.Session(req)
      req.content_type=3D"text/html"

      try:
            session['hits'] +=3D1
            #dirlist=3Dsession['sftp'].listdir('.')
            req.write('From Session')
      except:
            t=3Dparamiko.Transport(('[SERVER NAME],22))
            t.connect(username=3D'[USER NAME]',password=3D'[PASSWORD]')
            sftp=3Dparamiko.SFTPClient.from_transport(t)
            dirlist =3D sftp.listdir('.')
            session['hits']=3D1
            session['sftp']=3Dsftp
            req.write('New Session')

      session.save()
      req.write('Hits %d\n' % session['hits'])
      req.write(dirlist)

      return apache.OK


Mark Brown
Credit & Operations Manager
GreenSky
[email protected]<mailto:[email protected]>
Office: 404.832.4147
Fax: 404.832.4111


________________________________
This electronic message transmission contains information from GreenSky Tra=
de Credit, LLC which may be confidential & privileged. Recipients should no=
t file copies of this e-mail with publicly accessed records. The informatio=
n is intended to be for the use of the individual(s) named above. If you ar=
e not the intended recipient, please be aware that any disclosure, copying,=
 distribution or use of this message is prohibited. If you have received th=
is electronic transmission in error, please notify us by e-mail immediately=
.

--_000_0B1220B1BC53C04384C8EACA4EEA24C2814EE259A6GSEXCH01green_
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr=
osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:x=3D"urn:schemas-microsoft-com:office:excel" xmlns:p=3D"urn:schemas-m=
icrosoft-com:office:powerpoint" xmlns:a=3D"urn:schemas-microsoft-com:office=
:access" xmlns:dt=3D"uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:s=3D"=
uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:rs=3D"urn:schemas-microsof=
t-com:rowset" xmlns:z=3D"#RowsetSchema" xmlns:b=3D"urn:schemas-microsoft-co=
m:office:publisher" xmlns:ss=3D"urn:schemas-microsoft-com:office:spreadshee=
t" xmlns:c=3D"urn:schemas-microsoft-com:office:component:spreadsheet" xmlns=
:odc=3D"urn:schemas-microsoft-com:office:odc" xmlns:oa=3D"urn:schemas-micro=
soft-com:office:activation" xmlns:html=3D"http://www.w3.org/TR/REC-html40" =
xmlns:q=3D"http://schemas.xmlsoap.org/soap/envelope/" xmlns:rtc=3D"http://m=
icrosoft.com/officenet/conferencing" xmlns:D=3D"DAV:" xmlns:Repl=3D"http://=
schemas.microsoft.com/repl/" xmlns:mt=3D"http://schemas.microsoft.com/share=
point/soap/meetings/" xmlns:x2=3D"http://schemas.microsoft.com/office/excel=
/2003/xml" xmlns:ppda=3D"http://www.passport.com/NameSpace.xsd" xmlns:ois=
=3D"http://schemas.microsoft.com/sharepoint/soap/ois/" xmlns:dir=3D"http://=
schemas.microsoft.com/sharepoint/soap/directory/" xmlns:ds=3D"http://www.w3=
.org/2000/09/xmldsig#" xmlns:dsp=3D"http://schemas.microsoft.com/sharepoint=
/dsp" xmlns:udc=3D"http://schemas.microsoft.com/data/udc" xmlns:xsd=3D"http=
://www.w3.org/2001/XMLSchema" xmlns:sub=3D"http://schemas.microsoft.com/sha=
repoint/soap/2002/1/alerts/" xmlns:ec=3D"http://www.w3.org/2001/04/xmlenc#"=
 xmlns:sp=3D"http://schemas.microsoft.com/sharepoint/" xmlns:sps=3D"http://=
schemas.microsoft.com/sharepoint/soap/" xmlns:xsi=3D"http://www.w3.org/2001=
/XMLSchema-instance" xmlns:udcs=3D"http://schemas.microsoft.com/data/udc/so=
ap" xmlns:udcxf=3D"http://schemas.microsoft.com/data/udc/xmlfile" xmlns:udc=
p2p=3D"http://schemas.microsoft.com/data/udc/parttopart" xmlns:wf=3D"http:/=
/schemas.microsoft.com/sharepoint/soap/workflow/" xmlns:dsss=3D"http://sche=
mas.microsoft.com/office/2006/digsig-setup" xmlns:dssi=3D"http://schemas.mi=
crosoft.com/office/2006/digsig" xmlns:mdssi=3D"http://schemas.openxmlformat=
s.org/package/2006/digital-signature" xmlns:mver=3D"http://schemas.openxmlf=
ormats.org/markup-compatibility/2006" xmlns:m=3D"http://schemas.microsoft.c=
om/office/2004/12/omml" xmlns:mrels=3D"http://schemas.openxmlformats.org/pa=
ckage/2006/relationships" xmlns:spwp=3D"http://microsoft.com/sharepoint/web=
partpages" xmlns:ex12t=3D"http://schemas.microsoft.com/exchange/services/20=
06/types" xmlns:ex12m=3D"http://schemas.microsoft.com/exchange/services/200=
6/messages" xmlns:pptsl=3D"http://schemas.microsoft.com/sharepoint/soap/Sli=
deLibrary/" xmlns:spsl=3D"http://microsoft.com/webservices/SharePointPortal=
Server/PublishedLinksService" xmlns:Z=3D"urn:schemas-microsoft-com:" xmlns:=
st=3D"&#1;" xmlns=3D"http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"=
>
<meta name=3D"Generator" content=3D"Microsoft Word 12 (filtered medium)">
<style>
<!--
 /* Font Definitions */
 @font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
	{font-family:"Trebuchet MS";
	panose-1:2 11 6 3 2 2 2 2 2 4;}
@font-face
	{font-family:Consolas;
	panose-1:2 11 6 9 2 2 4 3 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{mso-style-priority:99;
	color:purple;
	text-decoration:underline;}
p.MsoPlainText, li.MsoPlainText, div.MsoPlainText
	{mso-style-priority:99;
	mso-style-link:"Plain Text Char";
	margin:0in;
	margin-bottom:.0001pt;
	font-size:10.5pt;
	font-family:Consolas;}
span.EmailStyle17
	{mso-style-type:personal-compose;
	font-family:"Calibri","sans-serif";
	color:windowtext;}
span.PlainTextChar
	{mso-style-name:"Plain Text Char";
	mso-style-priority:99;
	mso-style-link:"Plain Text";
	font-family:Consolas;}
.MsoChpDefault
	{mso-style-type:export-only;}
@page Section1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
	{page:Section1;}
-->
</style><!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext=3D"edit">
  <o:idmap v:ext=3D"edit" data=3D"1" />
 </o:shapelayout></xml><![endif]-->
</head>
<body lang=3D"EN-US" link=3D"blue" vlink=3D"purple">
<div class=3D"Section1">
<p class=3D"MsoNormal"><span lang=3D"EN-GB" style=3D"font-size:7.5pt">Sorry=
 that I am really, really new to Python but I have run into a wall and need=
 some help:<br>
<br>
Basically, I am trying to create a web-client for a sftp server. <o:p></o:p=
></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-GB" style=3D"font-size:7.5pt"><o:p>=
&nbsp;</o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-GB" style=3D"font-size:7.5pt">You e=
nter your user name and password and you authenticate against the sftp serv=
er, see a list of all the files on the server, can browse the server, and i=
f you click on a link download the file.<br>
<br>
<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-GB" style=3D"font-size:7.5pt">I hav=
e tried several different methods to keep the network-resource in the sessi=
on, 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.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-GB" style=3D"font-size:7.5pt"><o:p>=
&nbsp;</o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-GB" style=3D"font-size:7.5pt">I hav=
e found some articles on how to turn a python script into a daemon, but I n=
eed some help understanding what is really going.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-GB" style=3D"font-size:7.5pt">Anywa=
ys, the script&nbsp; I have tested thus far using mod_python is below:<o:p>=
</o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-GB" style=3D"font-size:7.5pt"><o:p>=
&nbsp;</o:p></span></p>
<p class=3D"MsoPlainText"><span style=3D"font-family:&quot;Courier New&quot=
;">from mod_python import apache<br>
from mod_python import Session<br>
import paramiko</span><o:p></o:p></p>
<p class=3D"MsoNormal"><span lang=3D"EN-GB" style=3D"font-size:7.5pt"><o:p>=
&nbsp;</o:p></span></p>
<p class=3D"MsoPlainText"><span style=3D"font-family:&quot;Courier New&quot=
;">def handler(req):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session=3DSession.Session(req)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; req.content_type=3D&quot;text/html&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session[=
'hits'] &#43;=3D1<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #dirlist=
=3Dsession['sftp'].listdir('.')<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; req.writ=
e('From Session')<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t=3Dpara=
miko.Transport(('[SERVER NAME],22))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.connec=
t(username=3D'[USER NAME]',password=3D'[PASSWORD]')<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sftp=3Dp=
aramiko.SFTPClient.from_transport(t)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dirlist =
=3D sftp.listdir('.')<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session[=
'hits']=3D1<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session[=
'sftp']=3Dsftp<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; req.writ=
e('New Session')<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session.save()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; req.write('Hits %d\n' % session['hits'])<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; req.write(dirlist)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return apache.OK</span><o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal"><b><span style=3D"font-size:10.0pt;font-family:&quot=
;Trebuchet MS&quot;,&quot;sans-serif&quot;;
color:black">Mark Brown</span></b><span style=3D"color:#1F497D"><o:p></o:p>=
</span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:10.0pt;font-family:&quot;Tr=
ebuchet MS&quot;,&quot;sans-serif&quot;;
color:#1F497D">Credit &amp; Operations Manager</span><span style=3D"font-si=
ze:
12.0pt;font-family:&quot;Times New Roman&quot;,&quot;serif&quot;;color:#1F4=
97D"><o:p></o:p></span></p>
<p class=3D"MsoNormal"><b><span style=3D"font-size:10.0pt;font-family:&quot=
;Trebuchet MS&quot;,&quot;sans-serif&quot;;
color:#00A850">GreenSky</span></b><span style=3D"color:#1F497D"><o:p></o:p>=
</span></p>
<p class=3D"MsoNormal"><span style=3D"color:#1F497D"><a href=3D"mailto:esmi=
[email protected]" title=3D"mailto:[email protected]"><span sty=
le=3D"font-size:10.0pt;font-family:&quot;Trebuchet MS&quot;,&quot;sans-seri=
f&quot;;color:blue">[email protected]</span></a></span><span st=
yle=3D"font-size:10.0pt;font-family:&quot;Trebuchet MS&quot;,&quot;sans-ser=
if&quot;;color:#1F497D">&nbsp;</span><span style=3D"font-size:10.0pt;font-f=
amily:&quot;Arial&quot;,&quot;sans-serif&quot;;color:blue">&nbsp;</span><sp=
an style=3D"color:#1F497D"><o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:10.0pt;font-family:&quot;Tr=
ebuchet MS&quot;,&quot;sans-serif&quot;;
color:black">Office: 404.832.4147<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:10.0pt;font-family:&quot;Tr=
ebuchet MS&quot;,&quot;sans-serif&quot;;
color:black">Fax: 404.832.4111</span><span style=3D"font-size:10.0pt;font-f=
amily:
&quot;Arial&quot;,&quot;sans-serif&quot;;color:blue"><o:p></o:p></span></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<br>
<hr>
<font face=3D"Arial" color=3D"Gray" size=3D"1">This electronic message tran=
smission contains information from GreenSky Trade Credit, LLC which may be =
confidential &amp; privileged. Recipients should not file copies of this e-=
mail with publicly accessed records. The information
 is intended to be for the use of the individual(s) named above. If you are=
 not the intended recipient, please be aware that any disclosure, copying, =
distribution or use of this message is prohibited. If you have received thi=
s electronic transmission in error,
 please notify us by e-mail immediately.<br>
</font>
</body>
</html>

--_000_0B1220B1BC53C04384C8EACA4EEA24C2814EE259A6GSEXCH01green_--

--===============0712758592==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Mod_python mailing list
[email protected]
http://mailman.modpython.org/mailman/listinfo/mod_python

--===============0712758592==--