Python实现CGI获取小文本文件
时间:2009-04-01 来源:hkebao
python cgi文件上传程序(修改)
import cgitbcgitb.enable()
import cgi, os, urllib, md5
print "Content-type: text/html"
print """<HTML>
<HEAD>
<TITLE>CGI File Example</TITLE></HEAD><BODY>"""
form = cgi.FieldStorage()
if form.has_key('file'):
fileitem = form['file']
if not fileitem.file:
print "Error: not a file upload.<P>"
else:
print "Got file: %s<P>" % cgi.escape(fileitem.filename)
m = md5.new()
size = 0
while 1:
data = fileitem.file.read(4096)
if not len(data):
break
size += len(data)
m.update(data)
filename=os.path.split(fileitem.filename) #get file name
#print filename
file(filename[1],'wb').write(data) #write file
print "Received file of %d bytes. MD5sum is %s<P>"% (size, m.hexdigest())
else:
print "No file found.<P>"
print """<FORM METHOD="POST" ACTION="%s" enctype="multipart/form-data">
File: <INPUT TYPE="file" NAME="file">
""" % os.environ['SCRIPT_NAME']
print """<INPUT TYPE="submit" NAME="submit" VALUE="Submit">
</FORM>
</BODY></HTML>"""
相关阅读 更多 +