Skip to content

Instantly share code, notes, and snippets.

@obskyr
Last active January 1, 2025 14:46
Show Gist options
  • Select an option

  • Save obskyr/b9d4b4223e7eaf4eedcd9defabb34f13 to your computer and use it in GitHub Desktop.

Select an option

Save obskyr/b9d4b4223e7eaf4eedcd9defabb34f13 to your computer and use it in GitHub Desktop.

Revisions

  1. obskyr revised this gist Mar 1, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion stream_response.py
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ def _load_until(self, goal_position):
    current_position = self._bytes.seek(0, SEEK_END)
    while current_position < goal_position:
    try:
    current_position = self._bytes.write(next(self._iterator))
    current_position += self._bytes.write(next(self._iterator))
    except StopIteration:
    break

  2. obskyr revised this gist Jan 4, 2021. 1 changed file with 31 additions and 0 deletions.
    31 changes: 31 additions & 0 deletions z_readme.md
    Original file line number Diff line number Diff line change
    @@ -2,3 +2,34 @@
    In many applications, you'd like to access a `requests` response as a file-like object, simply having `.read()`, `.seek()`, and `.tell()` as normal. Especially when you only want to *partially* download a file, it'd be extra convenient if you could use a normal file interface for it, loading as needed.

    This is a wrapper class for doing that. Only bytes you request will be loaded - see the example in the gist itself.

    ## License

    This piece of code is licensed under the Unlicense, which means it is in the public domain; free to use without attribution. Go ahead and use it for anything without worries!

    ```
    This is free and unencumbered software released into the public domain.
    Anyone is free to copy, modify, publish, use, compile, sell, or
    distribute this software, either in source code form or as a compiled
    binary, for any purpose, commercial or non-commercial, and by any
    means.
    In jurisdictions that recognize copyright laws, the author or authors
    of this software dedicate any and all copyright interest in the
    software to the public domain. We make this dedication for the benefit
    of the public at large and to the detriment of our heirs and
    successors. We intend this dedication to be an overt act of
    relinquishment in perpetuity of all present and future rights to this
    software under copyright law.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
    OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
    ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.
    For more information, please refer to <https://unlicense.org>
    ```
  3. obskyr revised this gist Jan 9, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion stream_response.py
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ def seek(self, position, whence=SEEK_SET):

    def main():
    # Use the class by providing a requests stream iterator.
    response = requests.get('http://example.com/')
    response = requests.get('http://example.com/', stream=True)
    # Chunk size of 64 bytes, in this case. Adapt to your use case.
    stream = ResponseStream(response.iter_content(64))

  4. obskyr revised this gist Dec 9, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion stream_response.py
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ def main():
    stream = ResponseStream(response.iter_content(64))

    # Now we can read the first 100 bytes (for example) of the file
    # without loading the rest of it. Of course, it's more useful whence
    # without loading the rest of it. Of course, it's more useful when
    # loading large files, like music images, or video. 😉
    # Seek and tell will also work as expected; important for some applications.
    stream.read(100)
  5. obskyr renamed this gist Dec 5, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. obskyr renamed this gist Dec 5, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. obskyr created this gist Dec 5, 2016.
    4 changes: 4 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    # Streaming a `requests` response as a file
    In many applications, you'd like to access a `requests` response as a file-like object, simply having `.read()`, `.seek()`, and `.tell()` as normal. Especially when you only want to *partially* download a file, it'd be extra convenient if you could use a normal file interface for it, loading as needed.

    This is a wrapper class for doing that. Only bytes you request will be loaded - see the example in the gist itself.
    57 changes: 57 additions & 0 deletions stream_response.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    # -*- coding: utf-8 -*-

    import requests
    from io import BytesIO, SEEK_SET, SEEK_END

    class ResponseStream(object):
    def __init__(self, request_iterator):
    self._bytes = BytesIO()
    self._iterator = request_iterator

    def _load_all(self):
    self._bytes.seek(0, SEEK_END)
    for chunk in self._iterator:
    self._bytes.write(chunk)

    def _load_until(self, goal_position):
    current_position = self._bytes.seek(0, SEEK_END)
    while current_position < goal_position:
    try:
    current_position = self._bytes.write(next(self._iterator))
    except StopIteration:
    break

    def tell(self):
    return self._bytes.tell()

    def read(self, size=None):
    left_off_at = self._bytes.tell()
    if size is None:
    self._load_all()
    else:
    goal_position = left_off_at + size
    self._load_until(goal_position)

    self._bytes.seek(left_off_at)
    return self._bytes.read(size)

    def seek(self, position, whence=SEEK_SET):
    if whence == SEEK_END:
    self._load_all()
    else:
    self._bytes.seek(position, whence)

    def main():
    # Use the class by providing a requests stream iterator.
    response = requests.get('http://example.com/')
    # Chunk size of 64 bytes, in this case. Adapt to your use case.
    stream = ResponseStream(response.iter_content(64))

    # Now we can read the first 100 bytes (for example) of the file
    # without loading the rest of it. Of course, it's more useful whence
    # loading large files, like music images, or video. 😉
    # Seek and tell will also work as expected; important for some applications.
    stream.read(100)

    if __name__ == '__main__':
    main()