Allow control of buffer size for TCPClient, improve error messages.

This commit is contained in:
Aldo Cortesi 2012-06-30 10:52:28 +12:00
parent 7480f87cd7
commit 67669a2a57
2 changed files with 7 additions and 4 deletions

View File

@ -260,15 +260,15 @@ def read_response(rfile, method, body_size_limit):
if len(parts) == 2: # handle missing message gracefully
parts.append("")
if not len(parts) == 3:
raise HttpError(502, "Invalid server response: %s."%line)
raise HttpError(502, "Invalid server response: %s"%repr(line))
proto, code, msg = parts
httpversion = parse_http_protocol(proto)
if httpversion is None:
raise HttpError(502, "Invalid HTTP version: %s."%httpversion)
raise HttpError(502, "Invalid HTTP version: %s"%repr(httpversion))
try:
code = int(code)
except ValueError:
raise HttpError(502, "Invalid server response: %s."%line)
raise HttpError(502, "Invalid server response: %s"%repr(line))
headers = read_headers(rfile)
if code >= 100 and code <= 199:
return read_response(rfile, method, body_size_limit)

View File

@ -50,6 +50,8 @@ class FileLike:
class TCPClient:
rbufsize = -1
wbufsize = -1
def __init__(self, host, port):
self.host, self.port = host, port
self.connection, self.rfile, self.wfile = None, None, None
@ -78,7 +80,8 @@ class TCPClient:
addr = socket.gethostbyname(self.host)
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect((addr, self.port))
self.rfile, self.wfile = connection.makefile('rb'), connection.makefile('wb')
self.rfile = connection.makefile('rb', self.rbufsize)
self.wfile = connection.makefile('wb', self.wbufsize)
except socket.error, err:
raise NetLibError('Error connecting to "%s": %s' % (self.host, err))
self.connection = connection