[tor-commits] [stem/master] Defaulting ControlSocket constructors to connect

atagar at torproject.org atagar at torproject.org
Tue Dec 13 18:10:36 UTC 2011


commit a584686dd01f12c8209ea7bc55019fec752eeaa2
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Dec 12 18:05:23 2011 -0800

    Defaulting ControlSocket constructors to connect
    
    ControlSocket users almost always want the socket to be connected initially to
    defaulting it that way.
---
 stem/connection.py |    6 ++++--
 stem/socket.py     |   20 ++++++++++++++++++--
 test/runner.py     |    2 --
 3 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/stem/connection.py b/stem/connection.py
index 938ab2f..07dc50e 100644
--- a/stem/connection.py
+++ b/stem/connection.py
@@ -314,8 +314,9 @@ def get_protocolinfo_by_port(control_addr = "127.0.0.1", control_port = 9051, ge
       socket
   """
   
+  control_socket = stem.socket.ControlPort(control_addr, control_port, False)
+  
   try:
-    control_socket = stem.socket.ControlPort(control_addr, control_port)
     control_socket.connect()
     control_socket.send("PROTOCOLINFO 1")
     protocolinfo_response = control_socket.recv()
@@ -355,8 +356,9 @@ def get_protocolinfo_by_socket(socket_path = "/var/run/tor/control", get_socket
       socket
   """
   
+  control_socket = stem.socket.ControlSocketFile(socket_path, False)
+  
   try:
-    control_socket = stem.socket.ControlSocketFile(socket_path)
     control_socket.connect()
     control_socket.send("PROTOCOLINFO 1")
     protocolinfo_response = control_socket.recv()
diff --git a/stem/socket.py b/stem/socket.py
index 1739aaa..2dcb8ac 100644
--- a/stem/socket.py
+++ b/stem/socket.py
@@ -237,18 +237,26 @@ class ControlPort(ControlSocket):
   option.
   """
   
-  def __init__(self, control_addr = "127.0.0.1", control_port = 9051):
+  def __init__(self, control_addr = "127.0.0.1", control_port = 9051, connect = True):
     """
     ControlPort constructor.
     
     Arguments:
       control_addr (str) - ip address of the controller
       control_port (int) - port number of the controller
+      connect (bool)     - connects to the socket if True, leaves it
+                           unconnected otherwise
+    
+    Raises:
+      stem.socket.SocketError if connect is True and we're unable to establish
+        a connection
     """
     
     ControlSocket.__init__(self)
     self._control_addr = control_addr
     self._control_port = control_port
+    
+    if connect: self.connect()
   
   def get_address(self):
     """
@@ -284,16 +292,24 @@ class ControlSocketFile(ControlSocket):
   option.
   """
   
-  def __init__(self, socket_path = "/var/run/tor/control"):
+  def __init__(self, socket_path = "/var/run/tor/control", connect = True):
     """
     ControlSocketFile constructor.
     
     Arguments:
       socket_path (str) - path where the control socket is located
+      connect (bool)     - connects to the socket if True, leaves it
+                           unconnected otherwise
+    
+    Raises:
+      stem.socket.SocketError if connect is True and we're unable to establish
+        a connection
     """
     
     ControlSocket.__init__(self)
     self._socket_path = socket_path
+    
+    if connect: self.connect()
   
   def get_socket_path(self):
     """
diff --git a/test/runner.py b/test/runner.py
index 6284464..566644b 100644
--- a/test/runner.py
+++ b/test/runner.py
@@ -345,8 +345,6 @@ class Runner:
       control_socket = stem.socket.ControlSocketFile(CONTROL_SOCKET_PATH)
     else: return None
     
-    control_socket.connect()
-    
     # TODO: replace with higher level authentication functions when we have them
     if authenticate:
       if OPT_COOKIE in conn_opts:





More information about the tor-commits mailing list