将文件从ssh服务器直接传输到客户端
问题描述:
我有一个ssh服务器用于在线存储我的文件。我需要使这些文件可以轻松下载,所以我使用paramiko库连接到ssh服务器(从我的python脚本),然后列出文件并显示在网页上。问题是我不想将文件下载到Web服务器的磁盘(没有足够的空间),然后将它们发送给客户端,而不是像读取文件一样,并吐出它像它可以在PHP中完成。将文件从ssh服务器直接传输到客户端
这样的事情在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;
?>
答
CGI脚本上stdout
其传递到Web服务器生成输出。输出以标题行,一个空白行(作为分隔符)和内容开始。
因此您必须将header()
调用更改为print
语句。和readfile($file)
应
print
print open("2007_SalaryReport.pdf","rb").read()
答
不是一个程序员的回答:不是的paramiko,与sshfs
安装与文件目录和服务文件,就好像它们是本地文件系统上
+0
我不是在web服务器上的根目录,所以不能安装软件(免费帐户) – voldyman
答案取决于您使用的Web框架。 Python有不同的API的几个不同的Web framkeworks。 – rocksportrocker
我目前正在尝试普通的CGI脚本,但我已经开放给webapp2,web.py和Django .. – voldyman