Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Lib/httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import re
import socket
from sys import py3kwarning
import collections
from urlparse import urlsplit
import warnings
with warnings.catch_warnings():
Expand Down Expand Up @@ -855,7 +856,15 @@ def send(self, data):
self.sock.sendall(datablock)
datablock = data.read(blocksize)
else:
self.sock.sendall(data)
try:
self.sock.sendall(data)
except TypeError:
if isinstance(data, collections.Iterable):
for d in data:
self.sock.sendall(d)
else:
raise TypeError("data should be a bytes-like object "
"or an iterable, got %r" % type(data))

def _output(self, s):
"""Add a line of output to the current request buffer.
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,22 @@ def test_send(self):
conn.send(StringIO.StringIO(expected))
self.assertEqual(expected, sock.data)

def test_send_iter(self):
expected = b'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \
b'Accept-Encoding: identity\r\nContent-Length: 11\r\n' \
b'\r\nonetwothree'

def body():
yield b"one"
yield b"two"
yield b"three"

conn = httplib.HTTPConnection('example.com')
sock = FakeSocket("")
conn.sock = sock
conn.request('GET', '/foo', body(), {'Content-Length': '11'})
self.assertEquals(sock.data, expected)

def test_chunked(self):
chunked_start = (
'HTTP/1.1 200 OK\r\n'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support iterable bodies in httplib.