Added a 'create_connect()' function to socket.py, which creates a

connection with an optional timeout, and modified httplib.py to
use this function in HTTPConnection. Applies patch 1676823.
This commit is contained in:
Facundo Batista 2007-03-23 18:54:07 +00:00
parent f102e24bd3
commit 07c78be0b4
7 changed files with 181 additions and 25 deletions

View file

@ -625,7 +625,8 @@ class HTTPConnection:
debuglevel = 0
strict = 0
def __init__(self, host, port=None, strict=None):
def __init__(self, host, port=None, strict=None, timeout=None):
self.timeout = timeout
self.sock = None
self._buffer = []
self.__response = None
@ -658,25 +659,7 @@ class HTTPConnection:
def connect(self):
"""Connect to the host and port specified in __init__."""
msg = "getaddrinfo returns an empty list"
for res in socket.getaddrinfo(self.host, self.port, 0,
socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
self.sock = socket.socket(af, socktype, proto)
if self.debuglevel > 0:
print "connect: (%s, %s)" % (self.host, self.port)
self.sock.connect(sa)
except socket.error, msg:
if self.debuglevel > 0:
print 'connect fail:', (self.host, self.port)
if self.sock:
self.sock.close()
self.sock = None
continue
break
if not self.sock:
raise socket.error, msg
self.sock = socket.create_connection((self.host,self.port), self.timeout)
def close(self):
"""Close the connection to the HTTP server."""