summaryrefslogtreecommitdiffstats
path: root/module/plugins
diff options
context:
space:
mode:
authorGravatar Walter Purcaro <vuolter@gmail.com> 2015-01-23 00:52:58 +0100
committerGravatar Walter Purcaro <vuolter@gmail.com> 2015-01-23 00:52:58 +0100
commitce27cfd75023d3408cbdd74cf797a5aea7428de7 (patch)
tree43e9d97ea6be91880d79761525b0c42b8da1844e /module/plugins
parentImprove container __pattern__ (diff)
downloadpyload-ce27cfd75023d3408cbdd74cf797a5aea7428de7.tar.xz
[ClickAndLoad] Improve create_connection
Diffstat (limited to 'module/plugins')
-rw-r--r--module/plugins/hooks/ClickAndLoad.py78
1 files changed, 46 insertions, 32 deletions
diff --git a/module/plugins/hooks/ClickAndLoad.py b/module/plugins/hooks/ClickAndLoad.py
index 370c40053..682639750 100644
--- a/module/plugins/hooks/ClickAndLoad.py
+++ b/module/plugins/hooks/ClickAndLoad.py
@@ -17,40 +17,55 @@ def forward(source, destination):
else:
destination.shutdown(socket.SHUT_WR)
-_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, socket.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
-
+
+#: socket.create_connection wrapper for python 2.5
+def create_connection(address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
+ source_address=None):
+ try:
+ return socket.create_connection(address, timeout, source_address)
+
+ except SyntaxError:
+ """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. If *source_address* is set it must be a tuple of (host, port)
+ for the socket to bind as a source address before making the connection.
+ An host of \'\' or port 0 tells the OS to use the default.
+ """
+
+ host, port = address
+ err = None
+ 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 socket._GLOBAL_DEFAULT_TIMEOUT:
+ sock.settimeout(timeout)
+ if source_address:
+ sock.bind(source_address)
+ sock.connect(sa)
+ return sock
+
+ except error as _:
+ err = _
+ if sock is not None:
+ sock.close()
+
+ if err is not None:
+ raise err
+ else:
+ raise error("getaddrinfo returns an empty list")
+
+
class ClickAndLoad(Hook):
__name__ = "ClickAndLoad"
__type__ = "hook"
- __version__ = "0.27"
+ __version__ = "0.28"
__config__ = [("activated", "bool", "Activated" , True ),
("port" , "int" , "Port" , 9666 ),
@@ -98,7 +113,6 @@ class ClickAndLoad(Hook):
except socket.error, e:
self.logError(e)
- self.server(ip, webport, cnlport)
finally:
dock_socket.close()