[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Minor improvements for connection examples



commit ec0c7eb0c3b96fc50ddc117bd83f4f6477c13f4c
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Sep 1 11:52:21 2013 -0700

    Minor improvements for connection examples
    
    Changes include...
    
    * more conventional example using the 'with' keyword
    * disconnecting the controller afterward
    * corrected indentation
---
 docs/faq.rst                                   |  114 +++++++++++++-----------
 docs/tutorials/the_little_relay_that_could.rst |    5 +-
 2 files changed, 67 insertions(+), 52 deletions(-)

diff --git a/docs/faq.rst b/docs/faq.rst
index d942c0e..b5b3a2a 100644
--- a/docs/faq.rst
+++ b/docs/faq.rst
@@ -121,18 +121,19 @@ Once you have Tor running and `properly configured <tutorials/the_little_relay_t
           sys.exit(1)  # unable to get a connection
 
         print "Tor is running version %s" % controller.get_version()
+        controller.close()
 
    ::
 
-     % python example.py 
-     Tor is running version 0.2.4.10-alpha-dev (git-8be6058d8f31e578)
+      % python example.py 
+      Tor is running version 0.2.4.10-alpha-dev (git-8be6058d8f31e578)
 
    ... or if Tor isn't running...
 
    ::
 
-     % python example.py 
-     [Errno 111] Connection refused
+      % python example.py 
+      [Errno 111] Connection refused
 
 #. :func:`stem.control.Controller.from_port` and :func:`stem.control.Controller.from_socket_file`
 
@@ -144,36 +145,49 @@ Once you have Tor running and `properly configured <tutorials/the_little_relay_t
 
    ::
 
-     import getpass
-     import sys
+      import getpass
+      import sys
 
-     import stem
-     import stem.connection
+      import stem
+      import stem.connection
 
-     from stem.control import Controller
+      from stem.control import Controller
 
-     if __name__ == '__main__':
-       try:
-         controller = Controller.from_port()
-       except stem.SocketError as exc:
-         print "Unable to connect to tor on port 9051: %s" % exc
-         sys.exit(1)
+      if __name__ == '__main__':
+        try:
+          controller = Controller.from_port()
+        except stem.SocketError as exc:
+          print "Unable to connect to tor on port 9051: %s" % exc
+          sys.exit(1)
+
+        try:
+          controller.authenticate()
+        except stem.connection.MissingPassword:
+          pw = getpass.getpass("Controller password: ")
+
+          try:
+            controller.authenticate(password = pw)
+          except stem.connection.PasswordAuthFailed:
+            print "Unable to authenticate, password is incorrect"
+            sys.exit(1)
+        except stem.connection.AuthenticationFailure as exc:
+          print "Unable to authenticate: %s" % exc
+          sys.exit(1)
 
-       try:
-         controller.authenticate()
-       except stem.connection.MissingPassword:
-         pw = getpass.getpass("Controller password: ")
+        print "Tor is running version %s" % controller.get_version()
+        controller.close()
 
-         try:
-           controller.authenticate(password = pw)
-         except stem.connection.PasswordAuthFailed:
-           print "Unable to authenticate, password is incorrect"
-           sys.exit(1)
-       except stem.connection.AuthenticationFailure as exc:
-         print "Unable to authenticate: %s" % exc
-         sys.exit(1)
+   If you're fine with allowing your script to raise exceptions then this can be more nicely done as...
 
-       print "Tor is running version %s" % controller.get_version()
+   ::
+
+      from stem.control import Controller
+
+      if __name__ == '__main__':
+        with Controller.from_port() as controller:
+          controller.authenticate()
+
+          print "Tor is running version %s" % controller.get_version()
 
 #. `Socket Module <api/socket.html>`_
 
@@ -185,32 +199,32 @@ Once you have Tor running and `properly configured <tutorials/the_little_relay_t
 
    ::
 
-     import stem
-     import stem.connection
-     import stem.socket
-
-     if __name__ == '__main__':
-       try:
-         control_socket = stem.socket.ControlPort(port = 9051)
-         stem.connection.authenticate(control_socket)
-       except stem.SocketError as exc:
-         print "Unable to connect to tor on port 9051: %s" % exc
-         sys.exit(1)
-       except stem.connection.AuthenticationFailure as exc:
-         print "Unable to authenticate: %s" % exc
-         sys.exit(1)
-
-       print "Issuing 'GETINFO version' query...\n"
-       control_socket.send('GETINFO version')
-       print control_socket.recv()
+      import stem
+      import stem.connection
+      import stem.socket
+
+      if __name__ == '__main__':
+        try:
+          control_socket = stem.socket.ControlPort(port = 9051)
+          stem.connection.authenticate(control_socket)
+        except stem.SocketError as exc:
+          print "Unable to connect to tor on port 9051: %s" % exc
+          sys.exit(1)
+        except stem.connection.AuthenticationFailure as exc:
+          print "Unable to authenticate: %s" % exc
+          sys.exit(1)
+
+        print "Issuing 'GETINFO version' query...\n"
+        control_socket.send('GETINFO version')
+        print control_socket.recv()
 
    ::
 
-     % python example.py 
-     Issuing 'GETINFO version' query...
+      % python example.py 
+      Issuing 'GETINFO version' query...
 
-     version=0.2.4.10-alpha-dev (git-8be6058d8f31e578)
-     OK
+      version=0.2.4.10-alpha-dev (git-8be6058d8f31e578)
+      OK
 
 .. _how_do_i_request_a_new_identity_from_tor:
 
diff --git a/docs/tutorials/the_little_relay_that_could.rst b/docs/tutorials/the_little_relay_that_could.rst
index 751c216..0fb00c1 100644
--- a/docs/tutorials/the_little_relay_that_could.rst
+++ b/docs/tutorials/the_little_relay_that_could.rst
@@ -42,8 +42,9 @@ the following configuration options...
 
 When you change your torrc you'll need to either restart Tor is issue a SIGHUP
 for the new settings to take effect. Now let's write a script that tells us how
-many bytes Tor has sent and received since it started. Note that there are a `few ways to connect to Tor <../faq.html#how-do-i-connect-to-tor>`_. If you're unfamiliar
-with the '**with**' keyword then see `here
+many bytes Tor has sent and received since it started. Note that there are a
+`few ways to connect to Tor <../faq.html#how-do-i-connect-to-tor>`_. If you're
+unfamiliar with the '**with**' keyword then see `here
 <../faq.html#what-is-that-with-keyword-i-keep-seeing-in-the-tutorials>`_...
 
 ::

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits