patternpythonMinor
Log probe requests of WiFi devices
Viewed 0 times
logdevicesproberequestswifi
Problem
I developed a program that collects probe requests from WiFi devices. It already works but I think that a programmer can improve it.
```
#!/usr/bin/env python
import platform
import threading
import signal
import sys
import time
import subprocess
import re
import os
import os.path
import argparse
import distutils.spawn
if (platform.system() == 'Windows'):
print "Windows does not seem to be supported, try it at your own risk!"
sys.exit(1)
formatString = "{0: 0)):
if (channel == 'all'):
channel = range(1, 13 if (osname == "Darwin") else 15)
switchThread = switchChannelThread(1, 'SwitchChannel', delay, channel)
switchThread.start()
elif c_single.match(channel):
channel = [int(channel)]
switchThread = switchChannelThread(1, 'SwitchChannel', delay, channel)
switchThread.start()
elif c_range.match(channel):
rchannel = channel.split('-')
schannel = int(rchannel[0])
echannel = int(rchannel[1])
if (schannel > echannel):
channel = range(echannel, schannel + 1)
else:
channel = range(schannel, echannel + 1)
switchThread = switchChannelThread(1, 'SwitchChannel', delay, channel)
switchThread.start()
elif c_list.match(channel + ','):
channel = channel.split(',')
channel = [int(i) for i in channel]
switchThread = switchChannelThread(1, 'SwitchChannel', delay, channel)
switchThread.start()
else:
print "Wrong channel/s specified!"
sys.exit(1)
else:
print "Wrong delay specified!"
sys.exit(1)
signal.signal(signal.SIGINT, signal_handler)
print "Running..."
# start tshark and read the results
displayFilter = "wlan.fcs_good==1 and not wlan_mgt.ssid==\\\"\\\"";
fieldParams = "-T fields -e wlan.sa -e wlan_mgt.ssid -Eseparator=,";
tsharkCommandLine = "{0} -i {1
```
#!/usr/bin/env python
import platform
import threading
import signal
import sys
import time
import subprocess
import re
import os
import os.path
import argparse
import distutils.spawn
if (platform.system() == 'Windows'):
print "Windows does not seem to be supported, try it at your own risk!"
sys.exit(1)
formatString = "{0: 0)):
if (channel == 'all'):
channel = range(1, 13 if (osname == "Darwin") else 15)
switchThread = switchChannelThread(1, 'SwitchChannel', delay, channel)
switchThread.start()
elif c_single.match(channel):
channel = [int(channel)]
switchThread = switchChannelThread(1, 'SwitchChannel', delay, channel)
switchThread.start()
elif c_range.match(channel):
rchannel = channel.split('-')
schannel = int(rchannel[0])
echannel = int(rchannel[1])
if (schannel > echannel):
channel = range(echannel, schannel + 1)
else:
channel = range(schannel, echannel + 1)
switchThread = switchChannelThread(1, 'SwitchChannel', delay, channel)
switchThread.start()
elif c_list.match(channel + ','):
channel = channel.split(',')
channel = [int(i) for i in channel]
switchThread = switchChannelThread(1, 'SwitchChannel', delay, channel)
switchThread.start()
else:
print "Wrong channel/s specified!"
sys.exit(1)
else:
print "Wrong delay specified!"
sys.exit(1)
signal.signal(signal.SIGINT, signal_handler)
print "Running..."
# start tshark and read the results
displayFilter = "wlan.fcs_good==1 and not wlan_mgt.ssid==\\\"\\\"";
fieldParams = "-T fields -e wlan.sa -e wlan_mgt.ssid -Eseparator=,";
tsharkCommandLine = "{0} -i {1
Solution
Some points on style.
Python has an official style guide which has a lot of information on how to properly layout your code.
-
Use newlines and indentations after
in all circumstances.)
-
Have an entry point to your program. Right now, your code has no entry point, it is just in the global namespace. Do this:
Python has an official style guide which has a lot of information on how to properly layout your code.
- Don't put
if,elif,while, etc. conditions in brackets (i.eif a == 2and notif (a == 2)) This mostly comes from other languages which require this.
-
Use newlines and indentations after
if, else, etc. (Don't do if condition: code(), doif condition:
code()in all circumstances.)
- Don't compare to Booleans. (
if condition == Truetoif conditionandif condition == Falsetoif not condition)
- Use
lower_case_with_underscoresfor variable names (see this)
- Boolean values should not be negative and be verbs or verb+noun (
is_stopping = Trueorstopping = True, notdont_stop = Falseornot_stopping = False, then doif not stoppingrather thanif not_stopping)
-
Have an entry point to your program. Right now, your code has no entry point, it is just in the global namespace. Do this:
#!shebang
imports
classes
function definitions
def main():
everything that is not an import, class or function
if __name__ == "__main__": # i.e. The module is not being imported
main()Code Snippets
if condition:
code()#!shebang
imports
classes
function definitions
def main():
everything that is not an import, class or function
if __name__ == "__main__": # i.e. The module is not being imported
main()Context
StackExchange Code Review Q#83756, answer score: 7
Revisions (0)
No revisions yet.