我正在为分块上传文件编写服务器端。所以有时我得到了MemoryError,我无法理解我做错了什么。 这里是我的python代码(带框架金字塔):
@view_config(route_name='fileupload',renderer='../upload.mako')
def upload_file(request):
session = request.session
if 'authorized' in session and session['authorized'] is False:
return HTTPForbidden()
try:
filename = request.params.get('filename')
print request.params.get('chunkindex')
datatmp = request.params.get('data')
data64 = datatmp[13:]
data = base64.b64decode(data64)
f = open('/home/somedirectory/' + filename , 'ab')
f.write(data)
f.close()
except Exception as e:
print e
return {}
错误追溯:
2015-07-24 17:57:36,630 ERROR [waitress][Dummy-5 340] Exception when serving /upload
Traceback (most recent call last):
File "/home/myusername/project25/local/lib/python2.7/site- packages/waitress-0.8.9-py2.7.egg/waitress/channel.py", line 337, in service
task.service()
File "/home/myusername/project25/local/lib/python2.7/site-packages/waitress-0.8.9-py2.7.egg/waitress/task.py", line 173, in service
self.execute()
File "/home/myusername/project25/local/lib/python2.7/site-packages/waitress-0.8.9-py2.7.egg/waitress/task.py", line 392, in execute
app_iter = self.channel.server.application(env, start_response)
File "/home/myusername/project25/local/lib/python2.7/site-packages/pyramid-1.5.6-py2.7.egg/pyramid/router.py", line 242, in __call__
response = self.invoke_subrequest(request, use_tweens=True)
File "/home/myusername/project25/local/lib/python2.7/site-packages/pyramid-1.5.6-py2.7.egg/pyramid/router.py", line 217, in invoke_subrequest
response = handle_request(request)
File "/home/myusername/project25/local/lib/python2.7/site-packages/pyramid_debugtoolbar-2.3-py2.7.egg/pyramid_debugtoolbar/toolbar.py", line 168, in toolbar_tween
toolbar = DebugToolbar(request, panel_classes, global_panel_classes)
File "/home/myusername/project25/local/lib/python2.7/site-packages/pyramid_debugtoolbar-2.3-py2.7.egg/pyramid_debugtoolbar/toolbar.py", line 49, in __init__
panel_inst = panel_class(request)
File "/home/myusername/project25/local/lib/python2.7/site-packages/pyramid_debugtoolbar-2.3-py2.7.egg/pyramid_debugtoolbar/panels/request_vars.py", line 30, in __init__
for k in request.POST],
File "/usr/lib/python2.7/pprint.py", line 67, in saferepr
return _safe_repr(object, {}, None, 0)[0]
File "/usr/lib/python2.7/pprint.py", line 323, in _safe_repr
rep = repr(object)
MemoryError
答案 0 :(得分:1)
Pyramid Debugtoolbar在尝试在POST请求中呈现变量时引发此异常。
您已在开发环境中启用此功能,请禁用它以使用您的production.ini运行您的应用程序。评估行为是否不同。
BTW:运行关于身份验证的金字塔教程以保护您对经过身份验证的用户的查看,不要以这种方式吞下异常并使用日志语句而不是打印。任何Pyramid docs / tuts都将应用这些技术。