I have some python that talks to a tor daemon, here it tells the daemon to shut down.
from stem import Signal
from stem.control import Controller
def shutDownTor():
with Controller.from_port(port=portNum) as controller:
controller.signal(Signal.SHUTDOWN)
I'm using a with
statement because the code I'm stealing from learning from does so too. The code works fine, but I'm wondering if there is any point to using the with
statement.
I know that when you use with
to open files it makes sure the file closes even if there's an Exception
or interrupt. But in this case it seems like all with
is doing is adding an un-necessary tab. The variable controller
is even left inside the namespace.
The Controller
class you import from stem
is a wrapper for ControlSocket
which is itself a wrapper around a socket connection to Tor protocol. So when you use with
in your code, you do so to open a connection with the given port. The same way the file
is open and closed, you will have to open and close the connection yourself if you want to get rid of with
.