[Jython-checkins] jython (2.5): Introducing socket.create_connection.

alan.kennedy jython-checkins at python.org
Wed Nov 2 10:47:50 CET 2011


http://hg.python.org/jython/rev/cc548bacc001
changeset:   6270:cc548bacc001
branch:      2.5
parent:      6268:826ec962e0c1
user:        Alan Kennedy <jython-dev at xhaus.com>
date:        Wed Nov 02 09:43:30 2011 +0000
summary:
  Introducing socket.create_connection.
This is required for 2.6, but is also a useful convenience method for 2.5 users.
Fixes http://bugs.jython.org/issue1788

files:
  Lib/socket.py |  29 +++++++++++++++++++++++++++++
  1 files changed, 29 insertions(+), 0 deletions(-)


diff --git a/Lib/socket.py b/Lib/socket.py
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -1554,6 +1554,35 @@
 
 _GLOBAL_DEFAULT_TIMEOUT = object()
 
+def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT):
+    """Connect to *address* and return the socket object.
+
+    Convenience function.  Connect to *address* (a 2-tuple ``(host,
+    port)``) and return the socket object.  Passing the optional
+    *timeout* parameter will set the timeout on the socket instance
+    before attempting to connect.  If no *timeout* is supplied, the
+    global default timeout setting returned by :func:`getdefaulttimeout`
+    is 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 _GLOBAL_DEFAULT_TIMEOUT:
+                sock.settimeout(timeout)
+            sock.connect(sa)
+            return sock
+
+        except error, msg:
+            if sock is not None:
+                sock.close()
+
+    raise error, msg
+
 # Define the SSL support
 
 class ssl:

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list