将文件从ssh服务器直接传输到客户端

时间:2011-09-21 10:36:11

标签: python http ssh web scp

我有一个ssh服务器,用于在线存储我的文件。我需要让这些文件可以轻松下载,所以我使用paramiko库连接到ssh服务器(从我的python脚本),然后列出文件和显示在网页上。问题是,我不想将文件下载到Web服务器的磁盘(没有足够的空间),然后将它们发送到客户端,而是执行类似读取文件和吐出的事情,就像可以在php中完成一样。

在python

中有类似的东西
<?php
// your file to upload
$file = '2007_SalaryReport.pdf';
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: application/pdf");
// tell file size
header('Content-length: '.filesize($file));
// set file name
header('Content-disposition: attachment; filename='.basename($file));
readfile($file);
// Exit script. So that no useless data is output-ed.
exit;
?>

2 个答案:

答案 0 :(得分:1)

不是程序员的答案:代替paramiko,使用sshfs的文档挂载目录,并像在本地文件系统上一样提供文档

http://fuse.sourceforge.net/sshfs.html

答案 1 :(得分:0)

cgi脚本在stdout上生成输出,该输出将传递到Web服务器。输出以标题行开头,一行为空行(作为分隔符),然后是内容。

因此,您必须将header()调用更改为print语句。 readfile($file)应替换为

 print
 print open("2007_SalaryReport.pdf","rb").read()

请参阅http://en.wikipedia.org/wiki/Common_Gateway_Interface