如何使用Xsendfile(Django)下载的url内容

时间:2015-11-06 09:31:04

标签: python django urllib2 webclient-download x-sendfile

我想使用mod-xsendfile(我已下载并安装)来保存网址,外部网页中的内容,我在urllib和urllib2中读取变量one_download.I' m new这并且不确定如何正确配置某些x-sendfile属性。在下面的代码中,我假设我可以将urllib内容直接放入one_download到xsendfile中,而不是将中间步骤保存到txt文件,然后将该txt文件传递给xsendfile。

import urllib2,urllib
def download_from_external_url(request):
    post_data = [('name','Dave'),]  
    # example url  
    #url = http://www.expressen.se/kronikorer/k-g-bergstrom/sexpartiuppgorelsen-rackte-inte--det-star-klart-nu/ - for example
    result = urllib2.urlopen(url, urllib.urlencode(post_data))

    print result
    one_download = result.read()
    # testprint content in one_download in shell 
     print one_download 



# pass content in one_download, in dict c,  to xsendfile
    c = {"one_download":one_download}
    c['Content-Disposition']= 'attachment; one_download=%s' %smart_str(one_download)
    c["X-Sendfile"] = one_download # <-- not working 

    return HttpResponse(json.dumps(c),'one_download_index.html', mimetype='application/force-download')

1 个答案:

答案 0 :(得分:0)

这不是X-Sendfile的用途;它用于提供您已经拥有的静态文件,而无需通过Django。由于您正在动态下载文件,并且无论如何它都在内存中,您也可以直接为它提供服务。