【发布时间】:2010-11-25 22:34:00
【问题描述】:
嘿,伙计,我正在尝试将旧的 php 脚本转换为 python,但运气不佳。
脚本的目的是提供一个文件,同时隐藏它的来源。以下是 php 的工作原理:
<?php
$filepath = "foo.mp3";
$filesize = filesize($filepath);
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// force download dialog
//header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition: attachment;filename="'.$filepath.'"');
header("Content-Transfer-Encoding: binary");
#header('Content-Type: audio/mpeg3');
header('Content-Length: '.$filesize);
@readfile($filepath);
exit(0);
?>
当我在 Python 中执行等价操作时,我得到一个零字节的下载。这是我正在尝试的:
#!/usr/bin/env python
# encoding: utf-8
import sys
import os
import cgitb; cgitb.enable()
filepath = "foo.mp3"
filesize = os.path.getsize(filepath)
print "Prama: no-cache"
print "Expires: 0"
print "Cache-Control: must-revalidate, post-check=0, pre-check=0"
print "Content-Type: application/octet-stream"
print "Content-Type: application/download"
print 'Content-Disposition: attachment;filename="'+filepath+'"'
print "Content-Transfer-Encoding: binary"
print 'Content-Length: '+str(filesize)
print #required blank line
open(filepath,"rb").read()
谁能帮帮我?
【问题讨论】: