[Python-Dev] Adding timeout option to httplib...connect()
Guido van Rossum
guido at python.org
Fri Feb 9 06:14:44 CET 2007
I recently needed to access an HTTP URL with a timeout. I ended up
monkey-patching httplib.HTTPConnection so that the connect() method
has an optional second paramer, timeout, defaulting to None; if not
None, a call to settimeout() is added right after successful creation
of the socket.
Does anybody else think this is a good idea? (Personally I think this
should've been done years ago. :-) Shall I check it into the head?
Index: httplib.py
===================================================================
--- httplib.py (revision 53456)
+++ httplib.py (working copy)
@@ -656,7 +656,7 @@
def set_debuglevel(self, level):
self.debuglevel = level
- def connect(self):
+ def connect(self, timeout=None):
"""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,
@@ -664,6 +664,8 @@
af, socktype, proto, canonname, sa = res
try:
self.sock = socket.socket(af, socktype, proto)
+ if timeout is not None:
+ self.sock.settimeout(timeout)
if self.debuglevel > 0:
print "connect: (%s, %s)" % (self.host, self.port)
self.sock.connect(sa)
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list