diff options
author | Senthil Kumaran <orsenthil@gmail.com> | 2010-12-19 10:49:52 (GMT) |
---|---|---|
committer | Senthil Kumaran <orsenthil@gmail.com> | 2010-12-19 10:49:52 (GMT) |
commit | 7bc0d872ddb023333acc05b7d038cdb74cfc047b (patch) | |
tree | 6225e5ecb0546a7826743510555c604751b238c7 /Lib/http/client.py | |
parent | 8a60e94802b04a838607b4aebba6fc8b70f7b87c (diff) | |
download | cpython-7bc0d872ddb023333acc05b7d038cdb74cfc047b.zip cpython-7bc0d872ddb023333acc05b7d038cdb74cfc047b.tar.gz cpython-7bc0d872ddb023333acc05b7d038cdb74cfc047b.tar.bz2 |
Issue3243 - Support iterable bodies in httplib. Patch contributions by Xuanji Li and Chris AtLee.
Diffstat (limited to 'Lib/http/client.py')
-rw-r--r-- | Lib/http/client.py | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/Lib/http/client.py b/Lib/http/client.py index 8ea75ce..8d62aa5 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -71,6 +71,7 @@ import email.message import io import os import socket +import collections from urllib.parse import urlsplit import warnings @@ -730,7 +731,11 @@ class HTTPConnection: self.__state = _CS_IDLE def send(self, data): - """Send `data' to the server.""" + """Send `data' to the server. + ``data`` can be a string object, a bytes object, an array object, a + file-like object that supports a .read() method, or an iterable object. + """ + if self.sock is None: if self.auto_open: self.connect() @@ -762,8 +767,16 @@ class HTTPConnection: if encode: datablock = datablock.encode("iso-8859-1") self.sock.sendall(datablock) - else: + + 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 byte-like object\ + or an iterable, got %r " % type(it)) def _output(self, s): """Add a line of output to the current request buffer. |