Django的文件上传失败偶尔 [英] Django file upload failing occasionally

查看:484
本文介绍了Django的文件上传失败偶尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我口的第一个Django 1.0.2应用程序与Apache + mod_python的上OSX / Leopard上运行3.3.1 + 2.6.1蟒(均在64位模式下运行),我遇到一个偶然的错误时,上传与Django开发服务器进行测试时,这不是present的文件。

I am trying to port my first Django 1.0.2 application to run on OSX/Leopard with Apache + mod_python 3.3.1 + python 2.6.1 (all running in 64-bit mode) and I am experiencing an occasional error when uploading a file that was not present when testing with the Django development server.

在code的上传类似于Django文档中描述的:

The code for the upload is similar to what described in the Django documentation:

class UploadFileForm(forms.Form):
    file  = forms.FileField()
    description = forms.CharField(max_length=100)
    notifygroup = forms.BooleanField(label='Notify Group?', required=False)

def upload_file(request, date, meetingid ):
    print date, meetingid
    if request.method == 'POST':
        print 'before reloading the form...'
        form = UploadFileForm(request.POST, request.FILES)
    print 'after reloading the form'
        if form.is_valid():
            try: 
                handle_uploaded_file(request.FILES['file'], request.REQUEST['date'], request.REQUEST['description'], form.cleaned_data['notifygroup'], meetingid )
            except:
                return render_to_response('uploaded.html', { 'message': 'Error! File not uploaded!' })
            return HttpResponseRedirect('/myapp/uploaded/')
    else:
        form = UploadFileForm()
    return render_to_response('upload.html', {'form': form, 'date':date, 'meetingid':meetingid})

这code正常工作正常,但有时(例如,​​每10上传一次),并在相当长的等待时间后,它失败,出现以下错误:

This code normally works correctly, but sometimes (say, once every 10 uploads) and after a fairly long waiting time, it fails with the following error:

IOError at /myapp/upload/2009-01-03/1
Client read error (Timeout?)
Request Method: POST
Request URL:    http://192.168.0.164/myapp/upload/2009-01-03/1
Exception Type: IOError
Exception Value:    
Client read error (Timeout?)
Exception Location: /Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py in read, line 406
Python Executable:  /usr/sbin/httpd
Python Version: 2.6.1
Python Path:    ['/djangoapps/myapp/', '/djangoapps/', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python26.zip', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/plat-darwin', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/plat-mac', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/lib-tk', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/lib-old', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/lib-dynload', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages']
Server time:    Sun, 4 Jan 2009 22:42:04 +0100

Environment:

Request Method: POST
Request URL: http://192.168.0.164/myapp/upload/2009-01-03/1
Django Version: 1.0.2 final
Python Version: 2.6.1
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'myapp.application1']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware')

Traceback:
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  86.                 response = callback(request, *callback_args, **callback_kwargs)
File "/djangoapps/myapp/../myapp/application1/views.py" in upload_file
  137.         form = UploadFileForm(request.POST, request.FILES)
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/core/handlers/modpython.py" in _get_post
  113.             self._load_post_and_files()
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/core/handlers/modpython.py" in _load_post_and_files
  87.                 self._post, self._files = self.parse_file_upload(self.META, self._req)
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/__init__.py" in parse_file_upload
  124.         return parser.parse()
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in parse
  134.             for item_type, meta_data, field_stream in Parser(stream, self._boundary):
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in __iter__
  607.         for sub_stream in boundarystream:
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in next
  421.             return LazyStream(BoundaryIter(self._stream, self._boundary))
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in __init__
  447.         unused_char = self._stream.read(1)
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in read
  300.         out = ''.join(parts())
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in parts
  293.                 chunk = self.next()
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in next
  315.             output = self._producer.next()
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in next
  376.             data = self.flo.read(self.chunk_size)
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in read
  406.         return self._file.read(num_bytes)

Exception Type: IOError at /myapp/upload/2009-01-03/1
Exception Value: Client read error (Timeout?)

我尝试使用mod_wsgi的并没有什么区别运行一切。

I tried to run everything using mod_wsgi and no difference.

有谁知道我在做什么错了?

Does anybody know what am I doing wrong?

在此先感谢您的帮助!

PPDO

=====

更新:

虽然我成功上传大文件(60+ MB),当它失败失败与上传的大小没有明显的关系,即其与10KB文件也失败了已成功之前上传。

Though I succeeded uploading large files (60+ MB), when it fails it fails with no evident relationship with the size of the upload, i.e. it fails also with 10kB files that have successfully been uploaded before.

推荐答案

使用的mod_wsgi使问题消失了Firefox浏览器。

Using mod_wsgi made the problem go away for Firefox.

限制我的研究到Apache和Safari之间的相互作用问题,我偶然发现的Apache这个bug报告的https:/ /bugs.webkit.org/show_bug.cgi?id=5760 描述非常相似,所发生的事情,这显然仍处于打开状态的东西。读这给了我的想法,试图关闭keepalive而且,虽然我需要更广泛地测试了一下,看来问题已经一去不复返了。

Limiting my research to an interaction problem between Apache and Safari, I stumbled upon this bug report for Apache https://bugs.webkit.org/show_bug.cgi?id=5760 that describes something very similar to what is happening and it is apparently still open. Reading this gave me the idea to try and disable the keepalive and, though I need to test it more extensively, it seems the problem is gone.

一个简单的:

BrowserMatch野生动物园nokeepalive

BrowserMatch "Safari" nokeepalive

在Apache配置奏效了。

in the Apache configuration did the trick.

这篇关于Django的文件上传失败偶尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆