Hi Jens!gerold hat geschrieben:Dieser Code erzeugt identische Files beim Aufruf mitCode: Alles auswählen
python skriptname.py > test2.tar
Wenn man die Kommentarzeichen vor den "print"-Anweisungen, die für das CGI notwendig sind, wieder entfernt, dann funktioniert es auch mit XAMPP unter Windows 2000.
Code: Alles auswählen
print "Content-Disposition: attachment; filename=test.tar"
print "Content-Transfer-Encoding: 8bit"
print "Content-Type: application/octet-stream;\n"
# force input/output to binary
if sys.platform == "win32":
import msvcrt
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
[...]
temp.seek(0)
Hier das gesamte Skript, genau so wie ich es getestet habe. Danach habe ich die Ergebnisse mit "cmp" vom Cygwin verglichen und zum Test mit WinZip geöffnet. Allerdings habe ich nur Textdateien komprimiert. Vielleicht versuche ich es später noch mit anderen Dateien, aber erst mal mache ich Schluß.
Code: Alles auswählen
#!C:\Python24\python.exe
# -*- coding: iso-8859-1 -*-
#~ print "Content-type: text/html; charset=utf-8\n"
#~ import cgitb;cgitb.enable()
import sys, os, posixpath, time, glob
from tempfile import NamedTemporaryFile
from tarfile import TarFile
temp = NamedTemporaryFile(prefix="PyDown_")
tar = TarFile(mode="w", fileobj=temp)
for item in glob.glob("*.txt"):
tar.add(item)
tar.close()
print "Content-Disposition: attachment; filename=test.tar"
#~ print 'Content-Transfer-Encoding: binary'
print "Content-Transfer-Encoding: 8bit"
print "Content-Type: application/octet-stream;\n"# charset=utf-8\n'
# force input/output to binary
if sys.platform == "win32":
import msvcrt
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
f = file("test.tar", "wb")
temp.seek(0)
while 1:
data = temp.read(2048)
if not data:
break
f.write(data)
sys.stdout.write(data)
f.close()
temp.close()
Gerold