python 中的 MemoryError 分块上传
MemoryError in python with chunked uploading
我正在为分块上传文件编写服务器端。所以有时我得到 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
Pyramid Debugtoolbar 在尝试呈现您的 POST 请求中的变量时引发此异常。
您在开发环境中启用了此功能,运行将您的应用程序与您的 production.ini 禁用它。评估行为是否不同。
顺便说一句:运行 关于身份验证的金字塔教程保护您对经过身份验证的用户的看法,不要以这种方式吞下异常并使用日志记录语句而不是打印。任何金字塔 docs/tuts 都会应用这些技术。
我正在为分块上传文件编写服务器端。所以有时我得到 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
Pyramid Debugtoolbar 在尝试呈现您的 POST 请求中的变量时引发此异常。
您在开发环境中启用了此功能,运行将您的应用程序与您的 production.ini 禁用它。评估行为是否不同。
顺便说一句:运行 关于身份验证的金字塔教程保护您对经过身份验证的用户的看法,不要以这种方式吞下异常并使用日志记录语句而不是打印。任何金字塔 docs/tuts 都会应用这些技术。