Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28711814
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Python/Ruby

2009-04-01 14:39:29

python cgi文件上传程序(修改)

import cgitb
cgitb.enable()
import cgi, os, urllib, md5
print "Content-type: text/html"
print
print """

CGI File Example"""
form = cgi.FieldStorage()
if form.has_key('file'):
    fileitem = form['file']
    if not fileitem.file:
        print "Error: not a file upload.

"
    else:
        print "Got file: %s

" % 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

"% (size, m.hexdigest())
else:
    print "No file found.

"
print """


File:
""" % os.environ['SCRIPT_NAME']
print """

"""

阅读(1284) | 评论(0) | 转发(0) |
0

上一篇:Python可参考一个示例代码

下一篇:学习一

给主人留下些什么吧!~~