Skip to content
Advertisement

How to convert an iterable to a stream?

If I’ve got an iterable containing strings, is there a simple way to turn it into a stream? I want to do something like this:

JavaScript

Advertisement

Answer

Here’s my streaming iterator an experimental branch of urllib3 supporting streaming chunked request via iterables:

JavaScript

Source with context: https://github.com/shazow/urllib3/blob/filepost-stream/urllib3/filepost.py#L23

Related unit tests: https://github.com/shazow/urllib3/blob/filepost-stream/test/test_filepost.py#L9

Alas this code hasn’t made it into the stable branch yet as sizeless chunked requests are poorly supported, but it should be a good foundation for what you’re trying to do. See the source link for examples showing how it can be used.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement