【问题标题】:linux+apache2.2+wsgi+python got extra content when uploading filelinux+apache2.2+wsgi+python上传文件时有额外内容
【发布时间】:2012-01-29 09:20:13
【问题描述】:

我设置了linux+apache2.2.+wsgi+python环境来测试文件上传。 共有2个页面,一是让用户选择要上传的文件;另一个是处理文件上传。

预期结果:
上传的文件内容正确。

实际结果:
上传了一个文件,但内容是原始文件内容加上 http 标头和开始/结束行的一部分。喜欢:

-----------------------------40976349392994148594600211
Content-Disposition: form-data; name="filename"; filename="configure.scan"
Content-Type: application/octet-stream
[original file content]
-----------------------------40976349392994148594600211

有人可以给我答案吗?我将深深感谢您的帮助。

首页代码

output= '<html><head>' +\  
        '<br>' + \  
        '</head><body>' + \  
        '<form name="form1" action=“/dynamic/postuploadfile.py” enctype="multipart/form-data" method=“post”>' +\  
        'File: <input type="file" name="test" size=50><br />' +\  
        '<input type=“submit” value="upload"/>' +\  
        '</form></body></html>'  

def application(environ, start_response):  
    status = '200 OK'  
    response_headers = [('Content-type', 'text/html'),  
                        ('Content-Length', str(len(output)))]  
    start_response(status, response_headers)  
    return [output]  

第二页代码:

import os  

def upload(environ):    

    # A nested FieldStorage instance holds the file  
    #fileitem = req.form['file']  
    data = environ['wsgi.input'].read(int(environ.get('CONTENT_LENGTH','0')))  

    message = ''  
    open('uploaded', 'wb').write(data)  
    message = 'The file was uploaded successfully'  

    return ( '<html><body>' + message + '</body><html>' )  

def application(environ, start_response):  
    status = '200 OK'  
    output = upload( environ )  
    response_headers = [('Content-type', 'text/html'),  
                        ('Content-Length', str(len(output)))]  
    start_response(status, response_headers)  
    return [output]   

【问题讨论】:

    标签: python linux wsgi


    【解决方案1】:

    是的。这是正确的行为。通过使用 mime 封装,可以上传多个文件。否则你会怎么做?

    看看cgi.FieldStorage 的python 类,它具有处理用于CGI 请求的multipart mime 类型的各种功能。

    不要天真地处理这个问题。在某些情况下,需要对文件内容进行编码或解码。最明显的情况是,当上传的文本文件本身包含分隔符字符串(在您的示例中为-----------------------------40976349392994148594600211 )时,需要以某种方式对其进行编码。

    你也可以试试 WSGI python 工具包 werkzeug,见http://werkzeug.pocoo.org/docs/http/#module-werkzeug.formparser

    【讨论】:

    • 感谢您的回答。如果在这种情况下,如何找出文件的真实内容在哪里。在上传文件之前,我不知道什么是分隔符字符串,比如 -----------------------------40976349392994148594600211,对吗?如果在上传文件之前进行编码,则不会出现此问题。
    • 这正是您应该使用现有解析器multipart上传中获取实际文件的原因。
    • 您建议使用哪些现有解析器来获取实际文件?是wsgi+python处理文件上传这么复杂,而PHP这么简单:move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]) ;
    • 你有没有试过我上面提到的email 包。我相信它可以解析 MIME 多部分内容。
    • 是的,我尝试了以下方式的电子邮件模块,但仍然得到相同的结果。 msg = email.message_from_string( data ) for part in msg.walk(): ctype = part.get_content_type() data1 = part.get_payload(decode=True) 上面的循环实际上只执行一次,data1 与 data 相同。不知道哪里错了。你能帮我解决这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多