ich versuche gerade nach einem Beispiel aus dem Netz einen File-Upload per
Python-CGI zu implementieren (momentan noch lokal). Der Transfer läuft durch und das Script
meldet einen erfolgreichen Upload - nur leider sind die Files (z.B.
jpeg-Bilder) danach viel kleiner und lassen sich nicht öffnen.
Server:
Code: Alles auswählen
import http.server
PORT = 8080
handler = http.server.CGIHTTPRequestHandler
httpd = http.server.HTTPServer(("", PORT), handler)
print("serving at port", PORT)
httpd.serve_forever()
Code: Alles auswählen
<div>
<form enctype="multipart/form-data" action="cgi-bin/upload.py" method="post">
<p>File: <input type="file" name="file"></p>
<input type="submit" value=" Start Upload ">
</form>
<div>
Code: Alles auswählen
#!C:\Python33\python.exe
#!/usr/bin/env python
import cgi, os
import cgitb; cgitb.enable()
try: # Windows needs stdio set for binary mode.
import msvcrt
msvcrt.setmode (0, os.O_BINARY) # stdin = 0
msvcrt.setmode (1, os.O_BINARY) # stdout = 1
except ImportError:
pass
form = cgi.FieldStorage()
# A nested FieldStorage instance holds the file
fileitem = form['file']
# Test if the file was uploaded
if fileitem.filename:
try:
fn = os.path.basename(fileitem.filename)
oFile = open('files\\' + fn, 'wb')
data = fileitem.value
oFile.write(data)
oFile.close()
message = '\nThe file "' + fn + '" was uploaded successfully'
except Exception as e:
message = 'Error wrting file "' + fn +'" : ' + str(e)
else:
message = 'No file was uploaded'
print("""\
Content-Type: text/html\n
<html><body>
<p>%s</p>
</body></html>
""" % (message,))
Code: Alles auswählen
fileitem.file.read()
Hat jemand eine Idee?