Use the "with" keyword when dealing with file objects
It is good practice to use the "with" keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks
This commit is contained in:
@@ -39,10 +39,8 @@ class Aircrack(object):
|
||||
def get_key_hex_ascii(self):
|
||||
if not self.is_cracked():
|
||||
raise Exception('Cracked file not found')
|
||||
f = open(self.cracked_file, 'r')
|
||||
hex_raw = f.read()
|
||||
f.close()
|
||||
|
||||
with open(self.cracked_file, 'r') as fid:
|
||||
hex_raw = fid.read()
|
||||
hex_key = ''
|
||||
ascii_key = ''
|
||||
while len(hex_raw) > 0:
|
||||
|
||||
@@ -96,14 +96,11 @@ class Aireplay(Thread):
|
||||
while self.pid.poll() is None:
|
||||
time.sleep(0.1)
|
||||
if not os.path.exists(self.output_file): continue
|
||||
# Read output file
|
||||
f = open(self.output_file, "r")
|
||||
lines = f.read()
|
||||
f.close()
|
||||
# Clear output file
|
||||
f = open(self.output_file, "w")
|
||||
f.write("")
|
||||
f.close()
|
||||
# Read output file & clear output file
|
||||
with open(self.output_file, "r+") as fid:
|
||||
lines = fid.read()
|
||||
fid.seek(0)
|
||||
fid.truncate()
|
||||
for line in lines.split("\n"):
|
||||
line = line.replace("\r", "").strip()
|
||||
if line == "": continue
|
||||
|
||||
@@ -189,9 +189,8 @@ class AttackWPA(Attack):
|
||||
Color.pl("")
|
||||
# Check crack result
|
||||
if os.path.exists(key_file):
|
||||
f = open(key_file, "r")
|
||||
key = f.read().strip()
|
||||
f.close()
|
||||
with open(key_file, "r") as fid:
|
||||
key = fid.read().strip()
|
||||
os.remove(key_file)
|
||||
|
||||
Color.pl("{+} {G}Cracked WPA Handshake{W} PSK: {G}%s{W}\n" % key)
|
||||
|
||||
@@ -27,17 +27,15 @@ class CrackResult(object):
|
||||
name = CrackResult.cracked_file
|
||||
json = []
|
||||
if os.path.exists(name):
|
||||
f = open(name, 'r')
|
||||
text = f.read()
|
||||
f.close()
|
||||
with open(name, 'r') as fid:
|
||||
text = fid.read()
|
||||
try:
|
||||
json = loads(text)
|
||||
except Exception, e:
|
||||
Color.pl('{!} error while loading %s: %s' % (name, str(e)))
|
||||
json.append(self.to_dict())
|
||||
f = open(name, 'w')
|
||||
f.write(dumps(json, indent=2))
|
||||
f.close()
|
||||
with open(name, 'w') as fid:
|
||||
fid.write(dumps(json, indent=2))
|
||||
Color.pl('{+} saved crack result to {C}%s{W} ({G}%d total{W})'
|
||||
% (name, len(json)))
|
||||
|
||||
|
||||
11
py/Reaver.py
11
py/Reaver.py
@@ -240,10 +240,8 @@ class Reaver(Attack):
|
||||
out = self.get_stdout()
|
||||
|
||||
# Clear output file
|
||||
f = open(self.stdout_file, 'w')
|
||||
f.write('')
|
||||
f.close()
|
||||
|
||||
with open(self.stdout_file, 'w'):
|
||||
pass
|
||||
# CHECK FOR CRACK
|
||||
|
||||
(pin, psk, ssid) = Reaver.get_pin_psk_ssid(out)
|
||||
@@ -394,9 +392,8 @@ class Reaver(Attack):
|
||||
''' Gets output from stdout_file '''
|
||||
if not self.stdout_file:
|
||||
return ''
|
||||
f = open(self.stdout_file, 'r')
|
||||
stdout = f.read()
|
||||
f.close()
|
||||
with open(self.stdout_file, 'r') as fid:
|
||||
stdout = fid.read()
|
||||
return stdout.strip()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user