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

[tor-commits] [ooni-probe/master] PEP8 cleanup



commit 6868e1df824ecd1b7899eef7580816fe0618b502
Author: kudrom <kudrom@xxxxxxxxxx>
Date:   Sat Jul 19 17:47:30 2014 +0200

    PEP8 cleanup
---
 ooni/templates/scapyt.py |   16 ++++++-----
 ooni/utils/txscapy.py    |   68 +++++++++++++++++++++++++++++++---------------
 2 files changed, 55 insertions(+), 29 deletions(-)

diff --git a/ooni/templates/scapyt.py b/ooni/templates/scapyt.py
index 8194925..ee21508 100644
--- a/ooni/templates/scapyt.py
+++ b/ooni/templates/scapyt.py
@@ -13,6 +13,7 @@ from ooni.settings import config
 from ooni.utils.txscapy import ScapySender, getDefaultIface, ScapyFactory
 from ooni.utils.txscapy import hasRawSocketPermission
 
+
 class BaseScapyTest(NetTestCase):
     """
     The report of a test run with scapy looks like this:
@@ -29,13 +30,13 @@ class BaseScapyTest(NetTestCase):
 
     requiresRoot = not hasRawSocketPermission()
     baseFlags = [
-            ['ipsrc', 's',
-                'Does *not* check if IP src and ICMP IP citation matches when processing answers'],
-            ['seqack', 'k',
-                'Check if TCP sequence number and ACK match in the ICMP citation when processing answers'],
-            ['ipid', 'i',
-                'Check if the IPID matches when processing answers']
-            ]
+        ['ipsrc', 's',
+         'Does *not* check if IP src and ICMP IP citation matches when processing answers'],
+        ['seqack', 'k',
+         'Check if TCP sequence number and ACK match in the ICMP citation when processing answers'],
+        ['ipid', 'i',
+         'Check if the IPID matches when processing answers']
+    ]
 
     def _setUp(self):
         super(BaseScapyTest, self)._setUp()
@@ -144,4 +145,5 @@ class BaseScapyTest(NetTestCase):
         for sent_packet in packets:
             self.report['sent_packets'].append(sent_packet)
 
+
 ScapyTest = BaseScapyTest
diff --git a/ooni/utils/txscapy.py b/ooni/utils/txscapy.py
index a96a397..bfd5d7f 100644
--- a/ooni/utils/txscapy.py
+++ b/ooni/utils/txscapy.py
@@ -19,9 +19,11 @@ from scapy.all import TCP, TCPerror, UDP, UDPerror
 from ooni.utils import log
 from ooni.settings import config
 
+
 class LibraryNotInstalledError(Exception):
     pass
 
+
 def pcapdnet_installed():
     """
     Checks to see if libdnet or libpcap are installed and set the according
@@ -39,8 +41,10 @@ def pcapdnet_installed():
     # expecting "dnet" so we try and import it under such name.
     try:
         import dumbnet
+
         sys.modules['dnet'] = dumbnet
-    except ImportError: pass
+    except ImportError:
+        pass
 
     try:
         conf.use_pcap = True
@@ -68,6 +72,7 @@ def pcapdnet_installed():
 
     return config.pcap_dnet
 
+
 if pcapdnet_installed():
     from scapy.all import PcapWriter
 
@@ -84,17 +89,18 @@ else:
 
 from scapy.all import Gen, SetGen, MTU
 
+
 def getNetworksFromRoutes():
     """ Return a list of networks from the routing table """
     from scapy.all import conf, ltoa, read_routes
-    from ipaddr    import IPNetwork, IPAddress
+    from ipaddr import IPNetwork, IPAddress
 
-    ## Hide the 'no routes' warnings
+    # # Hide the 'no routes' warnings
     conf.verb = 0
 
     networks = []
     for nw, nm, gw, iface, addr in read_routes():
-        n = IPNetwork( ltoa(nw) )
+        n = IPNetwork(ltoa(nw))
         (n.netmask, n.gateway, n.ipaddr) = [IPAddress(x) for x in [nm, gw, addr]]
         n.iface = iface
         if not n.compressed in networks:
@@ -102,12 +108,15 @@ def getNetworksFromRoutes():
 
     return networks
 
+
 class IfaceError(Exception):
     pass
 
+
 def getAddresses():
     from scapy.all import get_if_addr, get_if_list
     from ipaddr import IPAddress
+
     addresses = set()
     for i in get_if_list():
         try:
@@ -118,9 +127,10 @@ def getAddresses():
         addresses.remove('0.0.0.0')
     return [IPAddress(addr) for addr in addresses]
 
+
 def getDefaultIface():
     """ Return the default interface or raise IfaceError """
-    #XXX: currently broken on OpenVZ environments, because
+    # XXX: currently broken on OpenVZ environments, because
     # the routing table does not contain a default route
     # Workaround: Set the default interface in ooniprobe.conf
     networks = getNetworksFromRoutes()
@@ -129,6 +139,7 @@ def getDefaultIface():
             return net.iface
     raise IfaceError
 
+
 def hasRawSocketPermission():
     try:
         socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
@@ -136,17 +147,21 @@ def hasRawSocketPermission():
     except socket.error:
         return False
 
+
 class ProtocolNotRegistered(Exception):
     pass
 
+
 class ProtocolAlreadyRegistered(Exception):
     pass
 
+
 class ScapyFactory(abstract.FileDescriptor):
     """
     Inspired by muxTCP scapyLink:
     https://github.com/enki/muXTCP/blob/master/scapyLink.py
     """
+
     def __init__(self, interface, super_socket=None, timeout=5):
 
         abstract.FileDescriptor.__init__(self, reactor)
@@ -201,6 +216,7 @@ class ScapyFactory(abstract.FileDescriptor):
         else:
             raise ProtocolNotRegistered
 
+
 class ScapyProtocol(object):
     factory = None
 
@@ -213,6 +229,7 @@ class ScapyProtocol(object):
         """
         raise NotImplementedError
 
+
 class ScapySender(ScapyProtocol):
     timeout = 5
 
@@ -236,7 +253,7 @@ class ScapySender(ScapyProtocol):
             if packet.answers(answer_hr[i]):
                 self.answered_packets.append((answer_hr[i], packet))
                 if not self.multi:
-                    del(answer_hr[i])
+                    del (answer_hr[i])
                 break
 
         if len(self.answered_packets) == len(self.sent_packets):
@@ -245,7 +262,7 @@ class ScapySender(ScapyProtocol):
             return
 
         if self.expected_answers and \
-                self.expected_answers == len(self.answered_packets):
+                        self.expected_answers == len(self.answered_packets):
             log.debug("Got the number of expected answers")
             self.stopSending()
 
@@ -295,6 +312,7 @@ class ScapySender(ScapyProtocol):
         self.sendPackets(packets)
         return self.d
 
+
 class ScapySniffer(ScapyProtocol):
     def __init__(self, pcap_filename, *arg, **kw):
         self.pcapwriter = PcapWriter(pcap_filename, *arg, **kw)
@@ -302,6 +320,7 @@ class ScapySniffer(ScapyProtocol):
     def packetReceived(self, packet):
         self.pcapwriter.write(packet)
 
+
 class ParasiticTraceroute(ScapyProtocol):
     def __init__(self):
         self.numHosts = 7
@@ -332,7 +351,7 @@ class ParasiticTraceroute(ScapyProtocol):
                 return
             try:
                 packet[IP].ttl = self.hosts[packet.dst]['ttl'].pop()
-                del packet.chksum #XXX Why is this incorrect?
+                del packet.chksum  # XXX Why is this incorrect?
                 log.debug("Sent packet to %s with ttl %d" % (packet.dst, packet.ttl))
                 self.sendPacket(packet)
                 k = (packet.id, packet[TCP].sport, packet[TCP].dport, packet[TCP].seq)
@@ -345,10 +364,10 @@ class ParasiticTraceroute(ScapyProtocol):
         def maxttl(packet=None):
             if packet:
                 return min(self.ttl_max,
-                        min(
-                            abs( 64  - packet.ttl ),
-                            abs( 128 - packet.ttl ),
-                            abs( 256 - packet.ttl ))) - 1
+                           min(
+                               abs(64 - packet.ttl),
+                               abs(128 - packet.ttl),
+                               abs(256 - packet.ttl))) - 1
             else:
                 return self.ttl_max
 
@@ -362,15 +381,15 @@ class ParasiticTraceroute(ScapyProtocol):
                     and packet.dst not in self.addresses \
                     and isinstance(packet.getlayer(1), TCP):
 
-                self.hosts[packet.dst] = {'ttl' : genttl()}
+                self.hosts[packet.dst] = {'ttl': genttl()}
                 log.debug("Tracing to %s" % packet.dst)
 
             elif packet.src not in self.hosts \
                     and packet.src not in self.addresses \
                     and isinstance(packet.getlayer(1), TCP):
 
-                self.hosts[packet.src] = {'ttl' : genttl(packet),
-                        'ttl_max': maxttl(packet)}
+                self.hosts[packet.src] = {'ttl': genttl(packet),
+                                          'ttl_max': maxttl(packet)}
                 log.debug("Tracing to %s" % packet.src)
             return
 
@@ -387,6 +406,7 @@ class ParasiticTraceroute(ScapyProtocol):
     def stopListening(self):
         self.factory.unRegisterProtocol(self)
 
+
 class MPTraceroute(ScapyProtocol):
     dst_ports = [0, 22, 23, 53, 80, 123, 443, 8080, 65535]
     ttl_min = 1
@@ -408,7 +428,7 @@ class MPTraceroute(ScapyProtocol):
         d = defer.Deferred()
         reactor.callLater(self.timeout, d.callback, self)
 
-        self.sendPackets(IP(dst=host,ttl=(self.ttl_min,self.ttl_max), id=RandShort())/ICMP(id=RandShort()))
+        self.sendPackets(IP(dst=host, ttl=(self.ttl_min, self.ttl_max), id=RandShort()) / ICMP(id=RandShort()))
         return d
 
     def UDPTraceroute(self, host):
@@ -418,7 +438,8 @@ class MPTraceroute(ScapyProtocol):
         reactor.callLater(self.timeout, d.callback, self)
 
         for dst_port in self.dst_ports:
-            self.sendPackets(IP(dst=host,ttl=(self.ttl_min,self.ttl_max), id=RandShort())/UDP(dport=dst_port, sport=RandShort()))
+            self.sendPackets(
+                IP(dst=host, ttl=(self.ttl_min, self.ttl_max), id=RandShort()) / UDP(dport=dst_port, sport=RandShort()))
         return d
 
     def TCPTraceroute(self, host):
@@ -428,7 +449,10 @@ class MPTraceroute(ScapyProtocol):
         reactor.callLater(self.timeout, d.callback, self)
 
         for dst_port in self.dst_ports:
-            self.sendPackets(IP(dst=host,ttl=(self.ttl_min,self.ttl_max), id=RandShort())/TCP(flags=2L, dport=dst_port, sport=RandShort(), seq=RandShort()))
+            self.sendPackets(
+                IP(dst=host, ttl=(self.ttl_min, self.ttl_max), id=RandShort()) / TCP(flags=2L, dport=dst_port,
+                                                                                     sport=RandShort(),
+                                                                                     seq=RandShort()))
         return d
 
     @defer.inlineCallbacks
@@ -499,10 +523,10 @@ class MPTraceroute(ScapyProtocol):
             l = p.getlayer(1)
             i = 0
             if isinstance(l, ICMP):
-                i += matchResponse(('icmp', p.id), p) # match by ipid
-                i += matchResponse(('icmp', l.id), p) # match by icmpid
+                i += matchResponse(('icmp', p.id), p)  # match by ipid
+                i += matchResponse(('icmp', l.id), p)  # match by icmpid
             if isinstance(l, TCP):
-                i += matchResponse(('tcp', l.dport, l.sport), p) # match by s|dport 
+                i += matchResponse(('tcp', l.dport, l.sport), p)  # match by s|dport
                 i += matchResponse(('tcp', l.seq, l.sport, l.dport), p)
             if isinstance(l, UDP):
                 i += matchResponse(('udp', l.dport, l.sport), p)
@@ -517,7 +541,7 @@ class MPTraceroute(ScapyProtocol):
         if not l:
             return
         elif (isinstance(l, ICMP) or isinstance(l, UDP) or
-                isinstance(l, TCP)):
+                  isinstance(l, TCP)):
             self._recvbuf.append(packet)
 
     def stopListening(self):



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