deltaskelta deltaskelta - 1 year ago 166
Python Question

django how to download a file from the internet

I want to have a user input a file URL and then have my django app download the file from the internet.

My first instinct was to call wget inside my django app, but then I thought there may be another way to get this done. I couldn't find anything when I searched. Is there a more django way to do this?

Answer Source

You are not really dependent on Django for this. I happen to like using requests library.
Here is an example:

    import requests

    def download(url, path, chunk=2048):
        req = requests.get(url, stream=True)
        if req.status_code == 200:
            with open(path, 'wb') as f:
                for chunk in req.iter_content(chunk):
            return path
        raise Exception('Given url is return status code:{}'.format(req.status_code))

Place this is a file and import into your module whenever you need it.
Of course this is very minimal but this will get you started.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download