linux+apache2.2+wsgi+python在上传文件时获得了额外的内容
我设置了linux+apache2.2.+wsgi+python环境来测试文件上传。 共有2个页面,一个是让用户选择要上传的文件;另一个是处理文件上传 预期结果: 上载的文件包含正确的文件内容 实际结果: 文件已上载,但内容是原始文件内容加上部分http头和开始/结束行。比如:linux+apache2.2+wsgi+python在上传文件时获得了额外的内容,python,linux,wsgi,Python,Linux,Wsgi,我设置了linux+apache2.2.+wsgi+python环境来测试文件上传。 共有2个页面,一个是让用户选择要上传的文件;另一个是处理文件上传 预期结果: 上载的文件包含正确的文件内容 实际结果: 文件已上载,但内容是原始文件内容加上部分http头和开始/结束行。比如: -----------------------------40976349392994148594600211 Content-Disposition: form-data; name="filename"; filen
-----------------------------40976349392994148594600211
Content-Disposition: form-data; name="filename"; filename="configure.scan"
Content-Type: application/octet-stream
[original file content]
-----------------------------40976349392994148594600211
有人能给我答案吗?我将非常感谢你的帮助
第一页代码:
第二页代码:
对。这是正确的行为。通过使用mime封装,可以上载多个文件。否则你会怎么做
请看一看python的cgi.FieldStorage类,它具有处理cgi请求所用的多部分mime类型的各种功能
不要天真地处理这个问题。在某些情况下,需要对文件内容进行编码或解码。最明显的情况是,当上传的文本文件本身包含分隔符字符串------------------40976349392994148594600211时
在您的示例中,它需要以某种方式进行编码
您也可以尝试WSGI python工具包werkzeug,请参见谢谢您的回答。在这种情况下,如何确定文件的真实内容在哪里。在上传文件之前,我不知道什么是分隔符字符串,比如------------------40976349392994148594600211,对吗。如果在上载文件之前对其进行编码,则不会出现此问题。这正是您应该使用现有解析器从多部分上载中获取实际文件的原因。您建议使用哪些现有解析器来获取实际文件?wsgi+python处理文件上传是否如此复杂,而PHP是如此简单:move\u upload\u file$\u FILES[file][tmp\u name],upload/$_档案[档案][名称];你试过我上面提到的电子邮件包了吗。我相信它可以解析MIME多部分内容。是的,我用下面的方法尝试了电子邮件模块,但仍然得到了相同的结果。msg=email.message\u from\u msg.walk中部件的字符串数据:ctype=part.get\u content\u type data1=part.get\u payloaddecode=True上述循环实际上只执行一次,data1与数据相同。不知道哪里错了。你能帮我做这个吗。
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]