簡體   English   中英

將文件從SSH服務器直接傳輸到客戶端

[英]transfer files from ssh server directly to client

我有一個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;
?>

不是程序員的答案:代替paramiko,使用sshfs裝載帶有文件的目錄,並像處理本地文件系統一樣處理文件

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

一個cgi腳本在stdout上生成輸出,並將其輸出到Web服務器。 輸出從標題行開始,一個空白行(作為分隔符),然后是內容。

因此,您必須將header()調用更改為print語句。 並且readfile($file)應該替換為

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

參見http://en.wikipedia.org/wiki/Common_Gateway_Interface

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM