Skip to content

Commit a73b14e

Browse files
committed
Use 'with' statement to properly close the socket
1 parent dd6b956 commit a73b14e

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

bl_battery.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ def main():
7575
port = int(device[i+1:])
7676
device = device[:i]
7777
try:
78-
s = socket.socket(socket.AF_BLUETOOTH,
79-
socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
80-
s.connect((device, port))
81-
while getATCommand(s, s.recv(128), device, port):
82-
pass
78+
with socket.socket(socket.AF_BLUETOOTH,
79+
socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) as s:
80+
s.connect((device, port))
81+
while getATCommand(s, s.recv(128), device, port):
82+
pass
8383
except OSError as e:
8484
print(f"{device} is offline", e)
8585

0 commit comments

Comments
 (0)