Browse Source

Fix PEP8 errors

cypherpunks 9 years ago
parent
commit
45463f00ab
3 changed files with 97 additions and 117 deletions
  1. 2 1
      lib/chutney/Templating.py
  2. 73 94
      lib/chutney/TorNet.py
  3. 22 22
      lib/chutney/Traffic.py

+ 2 - 1
lib/chutney/Templating.py

@@ -144,7 +144,8 @@ class Environ(_DictWrapper):
        Environ, any keys not found in this Environ are searched for in
        Environ, any keys not found in this Environ are searched for in
        the parent.
        the parent.
 
 
-       >>> animal = Environ(mobile=True,legs=4,can_program=False,can_meow=False)
+       >>> animal = Environ(mobile=True,legs=4,can_program=False,
+                            can_meow=False)
        >>> biped = Environ(animal,legs=2)
        >>> biped = Environ(animal,legs=2)
        >>> cat = Environ(animal,can_meow=True)
        >>> cat = Environ(animal,can_meow=True)
        >>> human = Environ(biped,feathers=False,can_program=True)
        >>> human = Environ(biped,feathers=False,can_program=True)

+ 73 - 94
lib/chutney/TorNet.py

@@ -10,10 +10,7 @@
 from __future__ import print_function
 from __future__ import print_function
 from __future__ import with_statement
 from __future__ import with_statement
 
 
-# Get verbose tracebacks, so we can diagnose better.
 import cgitb
 import cgitb
-cgitb.enable(format="plain")
-
 import os
 import os
 import signal
 import signal
 import subprocess
 import subprocess
@@ -30,6 +27,9 @@ _BASE_ENVIRON = None
 _TORRC_OPTIONS = None
 _TORRC_OPTIONS = None
 _THE_NETWORK = None
 _THE_NETWORK = None
 
 
+# Get verbose tracebacks, so we can diagnose better.
+cgitb.enable(format="plain")
+
 
 
 def mkdir_p(d, mode=511):
 def mkdir_p(d, mode=511):
     """Create directory 'd' and all of its parents as needed.  Unlike
     """Create directory 'd' and all of its parents as needed.  Unlike
@@ -244,15 +244,14 @@ class LocalNodeBuilder(NodeBuilder):
                 "--list-torrc-options",
                 "--list-torrc-options",
                 "--hush"]
                 "--hush"]
             try:
             try:
-                opts = subprocess.check_output(cmdline,
-                                               bufsize=-1,
+                opts = subprocess.check_output(cmdline, bufsize=-1,
                                                universal_newlines=True)
                                                universal_newlines=True)
             except OSError as e:
             except OSError as e:
                 # only catch file not found error
                 # only catch file not found error
                 if e.errno == errno.ENOENT:
                 if e.errno == errno.ENOENT:
-                    print ("Cannot find tor binary %r. Use "
-                           "CHUTNEY_TOR environment variable to set the "
-                           "path, or put the binary into $PATH." % tor)
+                    print("Cannot find tor binary %r. Use "
+                          "CHUTNEY_TOR environment variable to set the "
+                          "path, or put the binary into $PATH." % tor)
                     sys.exit(0)
                     sys.exit(0)
                 else:
                 else:
                     raise
                     raise
@@ -278,15 +277,15 @@ class LocalNodeBuilder(NodeBuilder):
                 # check if the first word on the line is a supported option,
                 # check if the first word on the line is a supported option,
                 # preserving empty lines and comment lines
                 # preserving empty lines and comment lines
                 sline = line.strip()
                 sline = line.strip()
-                if (len(sline) == 0
-                    or sline[0] == '#'
-                    or sline.split()[0].lower() in lower_opts):
+                if (len(sline) == 0 or
+                        sline[0] == '#' or
+                        sline.split()[0].lower() in lower_opts):
                     f.writelines([line])
                     f.writelines([line])
                 else:
                 else:
                     # well, this could get spammy
                     # well, this could get spammy
                     # TODO: warn once per option per tor binary
                     # TODO: warn once per option per tor binary
                     # TODO: print tor version?
                     # TODO: print tor version?
-                    print (("The tor binary at %r does not support the "
+                    print(("The tor binary at %r does not support the "
                            "option in the torrc line:\n"
                            "option in the torrc line:\n"
                            "%r") % (tor, line.strip()))
                            "%r") % (tor, line.strip()))
                     # we could decide to skip these lines entirely
                     # we could decide to skip these lines entirely
@@ -341,11 +340,11 @@ class LocalNodeBuilder(NodeBuilder):
 
 
     def _makeHiddenServiceDir(self):
     def _makeHiddenServiceDir(self):
         """Create the hidden service subdirectory for this node.
         """Create the hidden service subdirectory for this node.
-          
+
           The directory name is stored under the 'hs_directory' environment
           The directory name is stored under the 'hs_directory' environment
           key. It is combined with the 'dir' data directory key to yield the
           key. It is combined with the 'dir' data directory key to yield the
           path to the hidden service directory.
           path to the hidden service directory.
-          
+
           448 is the decimal representation of the octal number 0700. Since
           448 is the decimal representation of the octal number 0700. Since
           python2 only supports 0700 and python3 only supports 0o700, we can
           python2 only supports 0700 and python3 only supports 0o700, we can
           use neither.
           use neither.
@@ -420,8 +419,8 @@ class LocalNodeBuilder(NodeBuilder):
         stdout, stderr = p.communicate()
         stdout, stderr = p.communicate()
         fingerprint = "".join((stdout.rstrip().split('\n')[-1]).split()[1:])
         fingerprint = "".join((stdout.rstrip().split('\n')[-1]).split()[1:])
         if not re.match(r'^[A-F0-9]{40}$', fingerprint):
         if not re.match(r'^[A-F0-9]{40}$', fingerprint):
-            print (("Error when calling %r. It gave %r as a fingerprint "
-                    " and %r on stderr.")%(" ".join(cmdline), stdout, stderr))
+            print(("Error when calling %r. It gave %r as a fingerprint "
+                   " and %r on stderr.") % (" ".join(cmdline), stdout, stderr))
             sys.exit(1)
             sys.exit(1)
         self._env['fingerprint'] = fingerprint
         self._env['fingerprint'] = fingerprint
 
 
@@ -601,18 +600,18 @@ class LocalNodeController(NodeController):
             sys.stdout.flush()
             sys.stdout.flush()
             time.sleep(self._env['poll_launch_time'])
             time.sleep(self._env['poll_launch_time'])
             p.poll()
             p.poll()
-        if p.returncode != None and p.returncode != 0:
+        if p.returncode is not None and p.returncode != 0:
             if self._env['poll_launch_time'] is None:
             if self._env['poll_launch_time'] is None:
                 print("Couldn't launch %s (%s): %s" % (self._env['nick'],
                 print("Couldn't launch %s (%s): %s" % (self._env['nick'],
                                                        " ".join(cmdline),
                                                        " ".join(cmdline),
                                                        p.returncode))
                                                        p.returncode))
             else:
             else:
                 print("Couldn't poll %s (%s) "
                 print("Couldn't poll %s (%s) "
-                       "after waiting %s seconds for launch"
-                       ": %s" % (self._env['nick'],
-                                  " ".join(cmdline),
-                                  self._env['poll_launch_time'],
-                                  p.returncode))
+                      "after waiting %s seconds for launch"
+                      ": %s" % (self._env['nick'],
+                                " ".join(cmdline),
+                                self._env['poll_launch_time'],
+                                p.returncode))
             return False
             return False
         return True
         return True
 
 
@@ -643,21 +642,22 @@ class LocalNodeController(NodeController):
                 if len(stline) > 0:
                 if len(stline) > 0:
                     splline = stline.split()
                     splline = stline.split()
                     # if the line has at least two tokens on it
                     # if the line has at least two tokens on it
-                    if (len(splline) > 0
-                        and splline[0].lower() == "RunAsDaemon".lower()
-                        and splline[1] == "1"):
+                    if (len(splline) > 0 and
+                            splline[0].lower() == "RunAsDaemon".lower() and
+                            splline[1] == "1"):
                         # use the RunAsDaemon value from the torrc
                         # use the RunAsDaemon value from the torrc
                         # TODO: multiple values?
                         # TODO: multiple values?
                         runAsDaemon = True
                         runAsDaemon = True
         if runAsDaemon:
         if runAsDaemon:
             # we must use wait() instead of poll()
             # we must use wait() instead of poll()
             self._env['poll_launch_time'] = None
             self._env['poll_launch_time'] = None
-            return True;
+            return True
         else:
         else:
             # we must use poll() instead of wait()
             # we must use poll() instead of wait()
             if self._env['poll_launch_time'] is None:
             if self._env['poll_launch_time'] is None:
-                self._env['poll_launch_time'] = self._env['poll_launch_time_default']
-            return False;
+                self._env['poll_launch_time'] = \
+                    self._env['poll_launch_time_default']
+            return False
 
 
 DEFAULTS = {
 DEFAULTS = {
     'authority': False,
     'authority': False,
@@ -685,11 +685,11 @@ DEFAULTS = {
     'authorities': "AlternateDirAuthority bleargh bad torrc file!",
     'authorities': "AlternateDirAuthority bleargh bad torrc file!",
     'bridges': "Bridge bleargh bad torrc file!",
     'bridges': "Bridge bleargh bad torrc file!",
     'core': True,
     'core': True,
-     # poll_launch_time: None means wait on launch (requires RunAsDaemon),
-     # otherwise, poll after that many seconds (can be fractional/decimal)
+    # poll_launch_time: None means wait on launch (requires RunAsDaemon),
+    # otherwise, poll after that many seconds (can be fractional/decimal)
     'poll_launch_time': None,
     'poll_launch_time': None,
-     # Used when poll_launch_time is None, but RunAsDaemon is not set
-     # Set low so that we don't interfere with the voting interval
+    # Used when poll_launch_time is None, but RunAsDaemon is not set
+    # Set low so that we don't interfere with the voting interval
     'poll_launch_time_default': 0.1,
     'poll_launch_time_default': 0.1,
     # the number of bytes of random data we send on each connection
     # the number of bytes of random data we send on each connection
     'data_bytes': int(os.environ.get('CHUTNEY_DATA_BYTES', 10 * 1024)),
     'data_bytes': int(os.environ.get('CHUTNEY_DATA_BYTES', 10 * 1024)),
@@ -777,8 +777,7 @@ class TorEnviron(chutney.Templating.Environ):
         if my['hs-hostname'] is None:
         if my['hs-hostname'] is None:
             datadir = my['dir']
             datadir = my['dir']
             # a file containing a single line with the hs' .onion address
             # a file containing a single line with the hs' .onion address
-            hs_hostname_file = os.path.join(datadir,
-                                            my['hs_directory'],
+            hs_hostname_file = os.path.join(datadir, my['hs_directory'],
                                             'hostname')
                                             'hostname')
             try:
             try:
                 with open(hs_hostname_file, 'r') as hostnamefp:
                 with open(hs_hostname_file, 'r') as hostnamefp:
@@ -787,8 +786,8 @@ class TorEnviron(chutney.Templating.Environ):
                 hostname = hostname.strip()
                 hostname = hostname.strip()
                 my['hs-hostname'] = hostname
                 my['hs-hostname'] = hostname
             except IOError as e:
             except IOError as e:
-                print("Error: hs %r error %d: %r opening hostname file '%r'"
-                      %(my['nick'], e.errno, e.strerror, hs_hostname_file))
+                print("Error: hs %r error %d: %r opening hostname file '%r'" %
+                      (my['nick'], e.errno, e.strerror, hs_hostname_file))
         return my['hs-hostname']
         return my['hs-hostname']
 
 
 
 
@@ -808,7 +807,7 @@ class Network(object):
         self._nodes.append(n)
         self._nodes.append(n)
 
 
     def move_aside_nodes(self):
     def move_aside_nodes(self):
-        nodesdir = os.path.join(os.getcwd(),'net','nodes')
+        nodesdir = os.path.join(os.getcwd(), 'net', 'nodes')
 
 
         if not os.path.exists(nodesdir):
         if not os.path.exists(nodesdir):
             return
             return
@@ -817,9 +816,9 @@ class Network(object):
         i = 0
         i = 0
         while os.path.exists(newdir):
         while os.path.exists(newdir):
             i += 1
             i += 1
-            newdir = "%s.%d" %(newdirbase, i)
+            newdir = "%s.%d" % (newdirbase, i)
 
 
-        print ("NOTE: renaming %r to %r"%(nodesdir, newdir))
+        print("NOTE: renaming %r to %r" % (nodesdir, newdir))
         os.rename(nodesdir, newdir)
         os.rename(nodesdir, newdir)
 
 
     def _checkConfig(self):
     def _checkConfig(self):
@@ -876,7 +875,6 @@ class Network(object):
         # line in wait()ing output
         # line in wait()ing output
         print("")
         print("")
         return rv
         return rv
-    
 
 
     def hup(self):
     def hup(self):
         print("Sending SIGHUP to nodes")
         print("Sending SIGHUP to nodes")
@@ -917,7 +915,7 @@ class Network(object):
     def _verify_traffic(self):
     def _verify_traffic(self):
         """Verify (parts of) the network by sending traffic through it
         """Verify (parts of) the network by sending traffic through it
         and verify what is received."""
         and verify what is received."""
-        LISTEN_PORT = 4747 # FIXME: Do better! Note the default exit policy.
+        LISTEN_PORT = 4747  # FIXME: Do better! Note the default exit policy.
         # HSs must have a HiddenServiceDir with
         # HSs must have a HiddenServiceDir with
         # "HiddenServicePort <HS_PORT> 127.0.0.1:<LISTEN_PORT>"
         # "HiddenServicePort <HS_PORT> 127.0.0.1:<LISTEN_PORT>"
         HS_PORT = 5858
         HS_PORT = 5858
@@ -927,8 +925,8 @@ class Network(object):
         # and a source-sink pair for a (bridge) client to each hidden service
         # and a source-sink pair for a (bridge) client to each hidden service
         DATALEN = self._dfltEnv['data_bytes']
         DATALEN = self._dfltEnv['data_bytes']
         # Print a dot each time a sink verifies this much data
         # Print a dot each time a sink verifies this much data
-        DOTDATALEN = 5 * 1024 * 1024 # Octets.
-        TIMEOUT = 3                  # Seconds.
+        DOTDATALEN = 5 * 1024 * 1024  # Octets.
+        TIMEOUT = 3                   # Seconds.
         # Calculate the amount of random data we should use
         # Calculate the amount of random data we should use
         randomlen = self._calculate_randomlen(DATALEN)
         randomlen = self._calculate_randomlen(DATALEN)
         reps = self._calculate_reps(DATALEN, randomlen)
         reps = self._calculate_reps(DATALEN, randomlen)
@@ -948,10 +946,7 @@ class Network(object):
             tmpdata = {}
             tmpdata = {}
         # now make the connections
         # now make the connections
         bind_to = ('127.0.0.1', LISTEN_PORT)
         bind_to = ('127.0.0.1', LISTEN_PORT)
-        tt = chutney.Traffic.TrafficTester(bind_to,
-                                           tmpdata,
-                                           TIMEOUT,
-                                           reps,
+        tt = chutney.Traffic.TrafficTester(bind_to, tmpdata, TIMEOUT, reps,
                                            dot_reps)
                                            dot_reps)
         client_list = filter(lambda n:
         client_list = filter(lambda n:
                              n._env['tag'] == 'c' or n._env['tag'] == 'bc',
                              n._env['tag'] == 'c' or n._env['tag'] == 'bc',
@@ -975,15 +970,12 @@ class Network(object):
         # if a node is used in two paths, we count it twice
         # if a node is used in two paths, we count it twice
         # this is a lower bound, as cannabilised circuits are one node longer
         # this is a lower bound, as cannabilised circuits are one node longer
         total_path_node_count = 0
         total_path_node_count = 0
-        total_path_node_count += self._configure_exits(tt, bind_to,
-                                                       tmpdata, reps,
-                                                       client_list, exit_list,
-                                                       LISTEN_PORT)
-        total_path_node_count += self._configure_hs(tt,
-                                                    tmpdata, reps,
+        total_path_node_count += self._configure_exits(tt, bind_to, tmpdata,
+                                                       reps, client_list,
+                                                       exit_list, LISTEN_PORT)
+        total_path_node_count += self._configure_hs(tt, tmpdata, reps,
                                                     client_list, hs_list,
                                                     client_list, hs_list,
-                                                    HS_PORT,
-                                                    LISTEN_PORT)
+                                                    HS_PORT, LISTEN_PORT)
         print("Transmitting Data:")
         print("Transmitting Data:")
         start_time = time.clock()
         start_time = time.clock()
         status = tt.run()
         status = tt.run()
@@ -1020,27 +1012,22 @@ class Network(object):
     # if there are any exits, each client / bridge client transmits
     # if there are any exits, each client / bridge client transmits
     # via 4 nodes (including the client) to an arbitrary exit
     # via 4 nodes (including the client) to an arbitrary exit
     # Each client binds directly to 127.0.0.1:LISTEN_PORT via an Exit relay
     # Each client binds directly to 127.0.0.1:LISTEN_PORT via an Exit relay
-    def _configure_exits(self, tt, bind_to,
-                         tmpdata, reps,
-                         client_list, exit_list,
-                         LISTEN_PORT):
+    def _configure_exits(self, tt, bind_to, tmpdata, reps, client_list,
+                         exit_list, LISTEN_PORT):
         CLIENT_EXIT_PATH_NODES = 4
         CLIENT_EXIT_PATH_NODES = 4
         connection_count = self._dfltEnv['connection_count']
         connection_count = self._dfltEnv['connection_count']
         exit_path_node_count = 0
         exit_path_node_count = 0
         if len(exit_list) > 0:
         if len(exit_list) > 0:
-            exit_path_node_count += (len(client_list)
-                                     * CLIENT_EXIT_PATH_NODES
-                                     * connection_count)
+            exit_path_node_count += (len(client_list) *
+                                     CLIENT_EXIT_PATH_NODES *
+                                     connection_count)
             for op in client_list:
             for op in client_list:
                 print("  Exit to %s:%d via client %s:%s"
                 print("  Exit to %s:%d via client %s:%s"
                       % ('127.0.0.1', LISTEN_PORT,
                       % ('127.0.0.1', LISTEN_PORT,
                          'localhost', op._env['socksport']))
                          'localhost', op._env['socksport']))
                 for i in range(connection_count):
                 for i in range(connection_count):
-                    tt.add(chutney.Traffic.Source(tt,
-                                                  bind_to,
-                                                  tmpdata,
-                                                  ('localhost',
-                                                   int(op._env['socksport'])),
+                    proxy = ('localhost', int(op._env['socksport']))
+                    tt.add(chutney.Traffic.Source(tt, bind_to, tmpdata, proxy,
                                                   reps))
                                                   reps))
         return exit_path_node_count
         return exit_path_node_count
 
 
@@ -1050,16 +1037,12 @@ class Network(object):
     # (including the client and hs) to each hidden service
     # (including the client and hs) to each hidden service
     # Instead of binding directly to LISTEN_PORT via an Exit relay,
     # Instead of binding directly to LISTEN_PORT via an Exit relay,
     # we bind to hs_hostname:HS_PORT via a hidden service connection
     # we bind to hs_hostname:HS_PORT via a hidden service connection
-    def _configure_hs(self, tt,
-                      tmpdata, reps,
-                      client_list, hs_list,
-                      HS_PORT,
+    def _configure_hs(self, tt, tmpdata, reps, client_list, hs_list, HS_PORT,
                       LISTEN_PORT):
                       LISTEN_PORT):
         CLIENT_HS_PATH_NODES = 8
         CLIENT_HS_PATH_NODES = 8
         connection_count = self._dfltEnv['connection_count']
         connection_count = self._dfltEnv['connection_count']
-        hs_path_node_count = (len(hs_list)
-                              * CLIENT_HS_PATH_NODES
-                              * connection_count)
+        hs_path_node_count = (len(hs_list) * CLIENT_HS_PATH_NODES *
+                              connection_count)
         # Each client in hs_client_list connects to each hs
         # Each client in hs_client_list connects to each hs
         if self._dfltEnv['hs_multi_client']:
         if self._dfltEnv['hs_multi_client']:
             hs_client_list = client_list
             hs_client_list = client_list
@@ -1073,15 +1056,12 @@ class Network(object):
             for client in hs_client_list:
             for client in hs_client_list:
                 print("  HS to %s:%d (%s:%d) via client %s:%s"
                 print("  HS to %s:%d (%s:%d) via client %s:%s"
                       % (hs._env['hs_hostname'], HS_PORT,
                       % (hs._env['hs_hostname'], HS_PORT,
-                     '127.0.0.1', LISTEN_PORT,
-                     'localhost', client._env['socksport']))
+                         '127.0.0.1', LISTEN_PORT,
+                         'localhost', client._env['socksport']))
                 for i in range(connection_count):
                 for i in range(connection_count):
-                    tt.add(chutney.Traffic.Source(tt,
-                                          hs_bind_to,
-                                          tmpdata,
-                                          ('localhost',
-                                           int(client._env['socksport'])),
-                                          reps))
+                    proxy = ('localhost', int(client._env['socksport']))
+                    tt.add(chutney.Traffic.Source(tt, hs_bind_to, tmpdata,
+                                                  proxy, reps))
         return hs_path_node_count
         return hs_path_node_count
 
 
     # calculate the single stream bandwidth and overall tor bandwidth
     # calculate the single stream bandwidth and overall tor bandwidth
@@ -1098,20 +1078,17 @@ class Network(object):
                           start_time, end_time):
                           start_time, end_time):
         # otherwise, if we sent at least 5 MB cumulative total, and
         # otherwise, if we sent at least 5 MB cumulative total, and
         # it took us at least a second to send, report bandwidth
         # it took us at least a second to send, report bandwidth
-        MIN_BWDATA = 5 * 1024 * 1024 # Octets.
-        MIN_ELAPSED_TIME = 1.0       # Seconds.
+        MIN_BWDATA = 5 * 1024 * 1024  # Octets.
+        MIN_ELAPSED_TIME = 1.0        # Seconds.
         cumulative_data_sent = total_path_node_count * data_length
         cumulative_data_sent = total_path_node_count * data_length
         elapsed_time = end_time - start_time
         elapsed_time = end_time - start_time
-        if (cumulative_data_sent >= MIN_BWDATA
-            and elapsed_time >= MIN_ELAPSED_TIME):
+        if (cumulative_data_sent >= MIN_BWDATA and
+                elapsed_time >= MIN_ELAPSED_TIME):
             # Report megabytes per second
             # Report megabytes per second
             BWDIVISOR = 1024*1024
             BWDIVISOR = 1024*1024
-            single_stream_bandwidth = (data_length
-                                       / elapsed_time
-                                       / BWDIVISOR)
-            overall_bandwidth = (cumulative_data_sent
-                                 / elapsed_time
-                                 / BWDIVISOR)
+            single_stream_bandwidth = (data_length / elapsed_time / BWDIVISOR)
+            overall_bandwidth = (cumulative_data_sent / elapsed_time /
+                                 BWDIVISOR)
             print("Single Stream Bandwidth: %.2f MBytes/s"
             print("Single Stream Bandwidth: %.2f MBytes/s"
                   % single_stream_bandwidth)
                   % single_stream_bandwidth)
             print("Overall tor Bandwidth: %.2f MBytes/s"
             print("Overall tor Bandwidth: %.2f MBytes/s"
@@ -1135,10 +1112,11 @@ def usage(network):
 
 
 
 
 def exit_on_error(err_msg):
 def exit_on_error(err_msg):
-    print ("Error: {0}\n".format(err_msg))
-    print (usage(_THE_NETWORK))
+    print("Error: {0}\n".format(err_msg))
+    print(usage(_THE_NETWORK))
     sys.exit(1)
     sys.exit(1)
 
 
+
 def runConfigFile(verb, data):
 def runConfigFile(verb, data):
     _GLOBALS = dict(_BASE_ENVIRON=_BASE_ENVIRON,
     _GLOBALS = dict(_BASE_ENVIRON=_BASE_ENVIRON,
                     Node=Node,
                     Node=Node,
@@ -1163,6 +1141,7 @@ def parseArgs():
         exit_on_error("Cannot find networkfile: {0}.".format(sys.argv[2]))
         exit_on_error("Cannot find networkfile: {0}.".format(sys.argv[2]))
     return {'network_cfg': sys.argv[2], 'action': sys.argv[1]}
     return {'network_cfg': sys.argv[2], 'action': sys.argv[1]}
 
 
+
 def main():
 def main():
     global _BASE_ENVIRON
     global _BASE_ENVIRON
     global _TORRC_OPTIONS
     global _TORRC_OPTIONS

+ 22 - 22
lib/chutney/Traffic.py

@@ -47,8 +47,8 @@ def socks_cmd(addr_port):
     SOCKSv4: https://en.wikipedia.org/wiki/SOCKS#Protocol
     SOCKSv4: https://en.wikipedia.org/wiki/SOCKS#Protocol
     SOCKSv5: RFC1928, RFC1929
     SOCKSv5: RFC1928, RFC1929
     """
     """
-    ver = 4                     # Only SOCKSv4 for now.
-    cmd = 1                     # Stream connection.
+    ver = 4  # Only SOCKSv4 for now.
+    cmd = 1  # Stream connection.
     user = '\x00'
     user = '\x00'
     dnsname = ''
     dnsname = ''
     host, port = addr_port
     host, port = addr_port
@@ -101,7 +101,7 @@ class Peer(object):
 
 
     def __init__(self, ptype, tt, s=None):
     def __init__(self, ptype, tt, s=None):
         self.type = ptype
         self.type = ptype
-        self.tt = tt            # TrafficTester
+        self.tt = tt  # TrafficTester
         if s is not None:
         if s is not None:
             self.s = s
             self.s = s
         else:
         else:
@@ -156,18 +156,17 @@ class Sink(Peer):
         # shortcut read when we don't ever expect any data
         # shortcut read when we don't ever expect any data
         if self.repetitions == 0 or len(self.tt.data) == 0:
         if self.repetitions == 0 or len(self.tt.data) == 0:
             debug("no verification required - no data")
             debug("no verification required - no data")
-            return 0;
+            return 0
         self.inbuf += self.s.recv(len(data) - len(self.inbuf))
         self.inbuf += self.s.recv(len(data) - len(self.inbuf))
         debug("successfully received (bytes=%d)" % len(self.inbuf))
         debug("successfully received (bytes=%d)" % len(self.inbuf))
         while len(self.inbuf) >= len(data):
         while len(self.inbuf) >= len(data):
             assert(len(self.inbuf) <= len(data) or self.repetitions > 1)
             assert(len(self.inbuf) <= len(data) or self.repetitions > 1)
             if self.inbuf[:len(data)] != data:
             if self.inbuf[:len(data)] != data:
                 debug("receive comparison failed (bytes=%d)" % len(data))
                 debug("receive comparison failed (bytes=%d)" % len(data))
-                return -1       # Failed verification.
+                return -1  # Failed verification.
             # if we're not debugging, print a dot every dot_repetitions reps
             # if we're not debugging, print a dot every dot_repetitions reps
-            elif (not debug_flag
-                  and self.tt.dot_repetitions > 0
-                  and self.repetitions % self.tt.dot_repetitions == 0):
+            elif (not debug_flag and self.tt.dot_repetitions > 0 and
+                  self.repetitions % self.tt.dot_repetitions == 0):
                 sys.stdout.write('.')
                 sys.stdout.write('.')
                 sys.stdout.flush()
                 sys.stdout.flush()
             # repeatedly check data against self.inbuf if required
             # repeatedly check data against self.inbuf if required
@@ -246,11 +245,11 @@ class Source(Peer):
                     return -1
                     return -1
             assert(8 - len(self.inbuf) > 0)
             assert(8 - len(self.inbuf) > 0)
             return 8 - len(self.inbuf)
             return 8 - len(self.inbuf)
-        return self.want_to_write()     # Keep us around for writing if needed
+        return self.want_to_write()  # Keep us around for writing if needed
 
 
     def want_to_write(self):
     def want_to_write(self):
-        return (self.state == self.CONNECTING or len(self.outbuf) > 0
-                or (self.repetitions > 0 and len(self.data) > 0))
+        return (self.state == self.CONNECTING or len(self.outbuf) > 0 or
+                (self.repetitions > 0 and len(self.data) > 0))
 
 
     def on_writable(self):
     def on_writable(self):
         """Invoked when the socket becomes writable.
         """Invoked when the socket becomes writable.
@@ -287,18 +286,19 @@ class Source(Peer):
         # it should print length of the data sent
         # it should print length of the data sent
         # but the code works as long as this doesn't keep on happening
         # but the code works as long as this doesn't keep on happening
         if n > 0:
         if n > 0:
-          debug("successfully sent (bytes=%d)" % n)
-          self._sent_no_bytes = 0
+            debug("successfully sent (bytes=%d)" % n)
+            self._sent_no_bytes = 0
         else:
         else:
-          debug("BUG: sent no bytes")
-          self._sent_no_bytes += 1
-          if self._sent_no_bytes >= 10:
-            print("Send no data %d times. Stalled." % (self._sent_no_bytes))
-            sys.exit(-1)
-          time.sleep(1)
+            debug("BUG: sent no bytes")
+            self._sent_no_bytes += 1
+            if self._sent_no_bytes >= 10:
+                print("Send no data %d times. Stalled." %
+                      (self._sent_no_bytes))
+                sys.exit(-1)
+            time.sleep(1)
         self.outbuf = self.outbuf[n:]
         self.outbuf = self.outbuf[n:]
         if self.state == self.CONNECTING_THROUGH_PROXY:
         if self.state == self.CONNECTING_THROUGH_PROXY:
-            return 1            # Keep us around.
+            return 1  # Keep us around.
         debug("bytes remaining on outbuf (bytes=%d)" % len(self.outbuf))
         debug("bytes remaining on outbuf (bytes=%d)" % len(self.outbuf))
         # calculate the actual length of data remaining, including reps
         # calculate the actual length of data remaining, including reps
         # When 0, we're being removed.
         # When 0, we're being removed.
@@ -336,7 +336,7 @@ class TrafficTester():
             self.data = {}
             self.data = {}
         self.dot_repetitions = dot_repetitions
         self.dot_repetitions = dot_repetitions
         debug("listener fd=%d" % self.listener.fd())
         debug("listener fd=%d" % self.listener.fd())
-        self.peers = {}         # fd:Peer
+        self.peers = {}  # fd:Peer
 
 
     def sinks(self):
     def sinks(self):
         return self.get_by_ptype(Peer.SINK)
         return self.get_by_ptype(Peer.SINK)
@@ -385,7 +385,7 @@ class TrafficTester():
                         print("verification failed!")
                         print("verification failed!")
                     self.remove(p)
                     self.remove(p)
 
 
-            for fd in sets[1]:             # writable fd's
+            for fd in sets[1]:  # writable fd's
                 p = self.peers.get(fd)
                 p = self.peers.get(fd)
                 if p is not None:  # Might have been removed above.
                 if p is not None:  # Might have been removed above.
                     n = p.on_writable()
                     n = p.on_writable()