开发者

How to make a file which is on local server download-able over HTTP in python?

开发者 https://www.devze.com 2023-01-16 23:44 出处:网络
Framework: Django Language: Python OS: Ubuntu For example, let us assume that I have a file \"xyz.pdf\" at \"/home/username/project/\". I have a webpage with download button. So if people click on th

Framework: Django Language: Python OS: Ubuntu

For example, let us assume that I have a file "xyz.pdf" at "/home/username/project/". I have a webpage with download button. So if people click on that download button, the file xyz.pdf should be downloaded.

What I have done,

  1. Created a webpage with download button with href as "download/" (as you all know this url does not matter much)
  2. it is redirected to urls.py and finds the appropriate view say for example "xyzdownload"
  3. at xyzdownload I have following code, response = urllib2.urlopen('./project/xyz.pd开发者_如何学编程f') html = response.read()

The error I am getting is unknown url type: ./project/xyz.pdf

Please let me know if you guys need more clarification. Thanks a lot


Generally, you'd want to just host the file in apache etc., and provide a link to the location. If the file is generated dynamically, or you want to "embargo" it, here's how you might do it with cherrypy:

@cherrypy.expose
def download(self, filename):
    """
    Download the specified XML file
    """

    # only if it's one of these files
    if filename not in "foo.xml bar.xml".split():
        return "Nice try"

    content = open("/path/to/media/" + filename).read()

    cherrypy.response.headers['Content-Type'] = 'application/xml'
    cherrypy.response.headers['Content-Length'] = len(content)
    disp = 'attachment; filename=%s' filename
    cherrypy.response.headers['Content-Disposition'] = disp
    cherrypy.response.headers['filename'] = 'application/xml'
    cherrypy.response.headers['pragma'] = ""
    cherrypy.response.headers['content-cache'] = ""

    return content

The link would be like this:

http://example.com/download/foo.xml


The error I am getting is unknown url type: ./project/xyz.pdf

That's simple: ./project/xyz.pdf is not a URL. Wikipedia says:

Every URL consists of some of the following: the scheme name (commonly called protocol), followed by a colon, then, depending on scheme, a hostname (alternatively, IP address), a port number, the path of the resource to be fetched or the program to be run, then, for programs such as Common Gateway Interface (CGI) scripts, a query string, and with HTML documents, an anchor (optional) for where the page should start to be displayed.

You didn't provide the scheme name and the hostname. However, I don't understand why you do this. Calling urlopen means: download this file on my pc. It doesn't mean "give the file to the user".

BTW, the xyzdownload view should be defined as @Ryan Ginstrom suggested you. Of course, you've to adapt the code to Django.

To read some examples, look at here. And of course, you should read this question because it's almost the same one you did.


you may try mechanize for text clicking and url followers by regex it

import mechanize

br = mechanize.Browser()

# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

# Want debugging messages?
#br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)

# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

# Download xyz.pdf ;)
f = br.retrieve('http://www.site.com/xyz.pdf')[0]
print f
fh = open(f)

# or you may try with click download just uncomment
# br.click_link(text_regex="download")

# or by url regex
# br.click_link(url_regex="project/xyz.pdf", nr=0)

and check huge resource and tutorial here http://wwwsearch.sourceforge.net/mechanize/

0

精彩评论

暂无评论...
验证码 换一张
取 消