Python 如何将下载的url内容与Xsendfile(Django)一起使用
我想使用我下载并安装的mod-xsendfile来保存URL、外部页面中的内容,这些内容是我在变量one_download中使用urllib和urllib2读入的。我对此不熟悉,不知道如何正确配置一些x-sendfile属性。在下面的代码中,我假设我可以将一次下载中的urllib内容直接放入xsendfile,而不是采取中间步骤将其保存到txt文件,然后将该txt文件传递到xsendfilePython 如何将下载的url内容与Xsendfile(Django)一起使用,python,django,urllib2,webclient-download,x-sendfile,Python,Django,Urllib2,Webclient Download,X Sendfile,我想使用我下载并安装的mod-xsendfile来保存URL、外部页面中的内容,这些内容是我在变量one_download中使用urllib和urllib2读入的。我对此不熟悉,不知道如何正确配置一些x-sendfile属性。在下面的代码中,我假设我可以将一次下载中的urllib内容直接放入xsendfile,而不是采取中间步骤将其保存到txt文件,然后将该txt文件传递到xsendfile import urllib2,urllib def download_from_external_url
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')
这不是X-Sendfile的用途;它用于提供磁盘上已有的静态文件,而无需通过Django。由于您是动态下载文件的,而且文件仍在内存中,因此您不妨直接为其提供服务。我也这么认为,但后来我看到了一些类似的解决方案。我想在保存到db中时,将从url下载的材料使用Tastypie,因此我考虑将它与xsendfile一起保存到db中,然后与Tastypie Web服务一起使用。我可以将urllib下载保存到txt文件中,然后使用xsendfile将其读入db?或者,它可能像直接保存到db类字段一样简单?好吧,现在这就没什么意义了。用xsendfile将其读入数据库是什么意思?xsendfile是从发送到客户端开始的,它与读取数据库无关。按照您的建议直接保存内容即可解决,谢谢。关于使用xsendfile将其读取到数据库,我尝试使用urllib和urllib的另一个示例遵循以下SO帖子,但我意识到我的想法很复杂,我主要关心的是,如果将url中的内容直接保存到db中,是否会出现格式错误,这就是我尝试使用xsendfile的原因