【问题标题】:django authentication for static files静态文件的django身份验证
【发布时间】:2011-10-04 20:21:58
【问题描述】:

我有一组静态文件(通过应用程序上传),如图像、视频等,需要提供给经过身份验证的用户(即他们的 cookie 在会话中注册为经过身份验证)。

这些文件是独立的,与 css、javaacript 等其他媒体静态文件没有任何关系。

鉴于我需要验证的静态文件相当大,我想知道为它们提供服务的最有效方式是什么(顺便说一句,我正在使用 wsgi)。

目前我有这个:

def return_large_file(request, p_filename):
    """                                                                         
    Send a file through Django without loading the whole file into              
    memory at once. The FileWrapper will turn the file object into an           
    iterator for chunks of 8KB.                                                 
    """
    if not os.path.exists(p_filename):
        raise Exception('File %s does not exist!')

    try:
        _content_type = mimetypes.guess_type(p_filename)
    except:
        _content_type = 'application/octet-stream'

    wrapper = FileWrapper(file(p_filename))
    response = HttpResponse(wrapper, content_type=_content_type)
    response['Content-Length'] = os.path.getsize(p_filename)
    return response

【问题讨论】:

    标签: django wsgi django-wsgi


    【解决方案1】:

    我目前正在使用类似于您上面使用的功能。

    我在想,一旦性能/效率成为问题,我会使用 Apache mod-auth-external 对给定文件进行自定义授权。

    请注意,我提供这个答案不是基于我的经验,而是我自己的研究引导我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2018-04-04
      • 2021-05-31
      • 2015-07-06
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多