I am trying to learn WHEN NOT to use:
- classes
- member variables
HERE IS THE CODE
access_point_detection_classes.py
from scapy.all import *
class Handler :
def __init__(self) :
self.wap_list = []
self.TempWAP = None
def process_packet_capture(self, packet_capture) :
for packet in packet_capture :
if packet.haslayer(Dot11) :
if packet.subtype == 8 :
bssid = packet.addr3
if packet.haslayer(Dot11Elt) :
p = packet[Dot11Elt]
while isinstance(p, Dot11Elt) :
if p.ID == 0 :
essid = p.info
if p.ID == 3 :
channel = ord(p.info)
p = p.payload
self.TempWAP = WirelessAccessPoint(bssid, essid, channel)
if self.check_for_duplicates() == False:
self.wap_list.append(self.TempWAP)
def check_for_duplicates(self) :
for w in self.wap_list :
if self.TempWAP.bssid == w.bssid :
return True
return False
class WirelessAccessPoint :
def __init__(self, bssid, essid, channel) :
self.bssid = bssid
self.essid = essid
self.channel = channel
access_point_detection.py
from scapy.all import *
from access_point_detection_classes_module import *
def main() :
H = Handler()
packet_capture = sniff(count = 50)
H.process_packet_capture(packet_capture)
for w in H.wap_list :
print w.bssid
print w.essid
print w.channel
print '++++++++++++++++++++++++'
# enable_managed_mode('wlan0')
if __name__ == "__main__" :
main()
HERE ARE MY THOUGHTS
I am mostly sure I know WHEN to use a class and member variables. Look at my second class WirelessAccessPoint
. The perfect need for a class in my opinion.
- I need multiple objects of this type
- Easier management of the data that comes along with this type
That's great, but look at my first class Handler
. To be honest, I mostly created it so I could practice OOP. It does come in handy when I need to access wap_list
. However, I initialize it without passing parameters for the constructor. Is that defeating the purpose? It seems that I could create all the methods of this class with only the self
parameter to them. For instance ( haha ) I could make packet_capture
a member variable of Handler
and have the method process_packet_capture()
take the parameter self
.
At this point I feel like if I do not set some ground rules for myself I will try to make everything a class and everything a member variable and I will never pass a parameter besides self
ever again!