pythondjangourllib2webclientx-sendfile

How to use downloaded url content with Xsendfile (Django)


I want to use mod - xsendfile (which I've downloaded and installed) to save content from urls, external pages, that I read in with urllib and urllib2 in the variable one_download.I'm new to this and not sure how to properly configure some of the x-sendfile properties. In the code below I assume that I can place the urllib content in one_download directly into xsendfile instead of taking a middle step as saving it to a txt file and then pass that txt - file to 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')

Solution

  • That's not what X-Sendfile is for; it's for serving static files you already have on disk without having to go through Django. Since you're downloading the file dynamically, and it's in memory anyway, you might as well serve it directly.