我想使用 mod - xsendfile(我已经下载并安装)来保存来自 url、外部页面的内容,我在变量 one_download 中使用 urllib 和 urllib2 读取这些内容。我是新手,不知道如何正确配置一些 x-sendfile 属性。在下面的代码中,我假设我可以将 one_download 中的 urllib 内容直接放入 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')