summaryrefslogtreecommitdiffstats
path: root/Lib/socket.py
diff options
context:
space:
mode:
authorFacundo Batista <facundobatista@gmail.com>2007-03-23 18:54:07 (GMT)
committerFacundo Batista <facundobatista@gmail.com>2007-03-23 18:54:07 (GMT)
commit07c78be0b4723deb62acebab50888cc59b1e4eb2 (patch)
tree0d1da08d12eafe4ac485889e1aac891e035c0696 /Lib/socket.py
parentf102e24bd34442026f4200a298a8b08d1deb3616 (diff)
downloadcpython-07c78be0b4723deb62acebab50888cc59b1e4eb2.zip
cpython-07c78be0b4723deb62acebab50888cc59b1e4eb2.tar.gz
cpython-07c78be0b4723deb62acebab50888cc59b1e4eb2.tar.bz2
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.
Diffstat (limited to 'Lib/socket.py')
-rw-r--r--Lib/socket.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/socket.py b/Lib/socket.py
index 0082e76..c03e884 100644
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -24,6 +24,7 @@ inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
ssl() -- secure socket layer support (only available if configured)
socket.getdefaulttimeout() -- get the default timeout value
socket.setdefaulttimeout() -- set the default timeout value
+create_connection() -- connects to an address, with an optional timeout
[*] not available on all platforms!
@@ -412,3 +413,31 @@ class _fileobject(object):
if not line:
raise StopIteration
return line
+
+
+def create_connection(address, timeout=None):
+ """Connect to address (host, port) with an optional timeout.
+
+ Provides access to socketobject timeout for higher-level
+ protocols. Passing a timeout will set the timeout on the
+ socket instance (if not present, or passed as None, the
+ default global timeout setting will be used).
+ """
+
+ msg = "getaddrinfo returns an empty list"
+ host, port = address
+ for res in getaddrinfo(host, port, 0, SOCK_STREAM):
+ af, socktype, proto, canonname, sa = res
+ sock = None
+ try:
+ sock = socket(af, socktype, proto)
+ if timeout is not None:
+ sock.settimeout(timeout)
+ sock.connect(sa)
+ return sock
+
+ except error, msg:
+ if sock is not None:
+ sock.close()
+
+ raise error, msg