summaryrefslogtreecommitdiff
path: root/Lib/httplib.py
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2006-11-12 10:32:47 +0000
committerMartin v. Löwis <martin@v.loewis.de>2006-11-12 10:32:47 +0000
commitb8a590846634a5525b82a78e3bf2e8c14e7d541c (patch)
tree066459ac186bb9494b5c2c099ca10dcf97b84519 /Lib/httplib.py
parent7bb367865d92ffd641b913d4e83b7d9d128895de (diff)
downloadcpython-b8a590846634a5525b82a78e3bf2e8c14e7d541c.tar.gz
Patch #1065257: Support passing open files as body in
HTTPConnection.request().
Diffstat (limited to 'Lib/httplib.py')
-rw-r--r--Lib/httplib.py26
1 files changed, 24 insertions, 2 deletions
diff --git a/Lib/httplib.py b/Lib/httplib.py
index 5ae5efc916..1e0037f8f8 100644
--- a/Lib/httplib.py
+++ b/Lib/httplib.py
@@ -704,7 +704,15 @@ class HTTPConnection:
if self.debuglevel > 0:
print "send:", repr(str)
try:
- self.sock.sendall(str)
+ blocksize=8192
+ if hasattr(str,'read') :
+ if self.debuglevel > 0: print "sendIng a read()able"
+ data=str.read(blocksize)
+ while data:
+ self.sock.sendall(data)
+ data=str.read(blocksize)
+ else:
+ self.sock.sendall(str)
except socket.error, v:
if v[0] == 32: # Broken pipe
self.close()
@@ -879,7 +887,21 @@ class HTTPConnection:
self.putrequest(method, url, **skips)
if body and ('content-length' not in header_names):
- self.putheader('Content-Length', str(len(body)))
+ thelen=None
+ try:
+ thelen=str(len(body))
+ except TypeError, te:
+ # If this is a file-like object, try to
+ # fstat its file descriptor
+ import os
+ try:
+ thelen = str(os.fstat(body.fileno()).st_size)
+ except (AttributeError, OSError):
+ # Don't send a length if this failed
+ if self.debuglevel > 0: print "Cannot stat!!"
+
+ if thelen is not None:
+ self.putheader('Content-Length',thelen)
for hdr, value in headers.iteritems():
self.putheader(hdr, value)
self.endheaders()