【问题标题】:How to use downloaded url content with Xsendfile (Django)如何将下载的 url 内容与 Xsendfile (Django) 一起使用
【发布时间】:2015-11-06 09:31:03
【问题描述】:

我想使用 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')

【问题讨论】:

    标签: python django urllib2 webclient x-sendfile


    【解决方案1】:

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

    【讨论】:

    • 我是这么认为的,但后来我看到了一些类似的解决方案。当保存在数据库中时,我想将 Tastypie 用于从 url 下载中下载的材料,所以我想用 xsendfile 将它保存到数据库中,然后将它与 Tastypie webbservice 一起使用。我可以将 urllib 下载保存到 txt 文件,然后使用 xsendfile 将其读入 db?或者也许就像直接将其保存到 db 类字段一样简单?
    • 好吧,现在这更没有意义了。 “使用 xsendfile 将其读入数据库”是什么意思? xsendfile 是从发送到客户端,它与“读入数据库”无关。
    • 按照您的建议直接保存内容解决了,谢谢。
    • 关于“使用 xsendfile 将其读入数据库”我尝试使用 urllib 遵循以下 SO 帖子以及使用 urllib 的另一个示例,但我意识到我对此的想法很复杂,我主要是担心如果将内容从 url 直接保存到数据库中是否会出现格式错误,这就是我尝试使用 xsendfile stackoverflow.com/questions/1156246/… 的原因
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    相关资源
    最近更新 更多