Fixed #18972 - Used xrange to chunk data

This makes the code more `modern` although imo the whole thing could do without checking for length and just do in either case

```python
for offset in xrange(0, length, max_chunk_size):
    self._write(data[offset:offset+max_chunk_size])
    self._flush()
```