Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper method analogous to urllib.urlretrieve #1632

Closed
hickford opened this issue Sep 27, 2013 · 3 comments

Comments

@hickford
Copy link

commented Sep 27, 2013

The method urllib.urlretrieve downloads a file from a URL and saves it to disk.

Copy a network object denoted by a URL to a local file, if necessary. If the URL points to a local file, or a valid cached copy of the object exists, the object is not copied.

urlretrieve() will raise ContentTooShortError when it detects that the amount of data available was less than the expected amount (which is the size reported by a Content-Length header). This can occur, for example, when the download is interrupted.

That's a useful helper method. Is there anything like this in Requests?

@Lukasa

This comment has been minimized.

Copy link
Member

commented Sep 28, 2013

There is no such analogous method, and the core library shouldn't have one. =) Instead, it should make writing one easy, which it does:

def urlretrieve(path, url):
    with open(path, 'wb') as f:
        r = requests.get(url, stream=True)
        r.raise_for_status()  # Replace this with better error handling.

        for chunk in r.iter_content(1024):
            f.write(chunk)

Thanks for suggesting this though! I might create a repository with useful snippets for Requests, in case people find the need for things like this with some frequency.

@Lukasa Lukasa closed this Sep 28, 2013

@sigmavirus24

This comment has been minimized.

Copy link
Contributor

commented Sep 28, 2013

Further more @matt-hickford in order to use requests for local files you must define your own file adapter. We have been asked for it several times and I believe we all three maintain the opinion that trying to use requests for local files makes absolutely no sense.

@hickford

This comment has been minimized.

Copy link
Author

commented Sep 28, 2013

Makes sense. Thanks for the snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.