Skip to content

Commit b95fdc7

Browse files
authored
Improve exception handling in PeriodicSender Thread (#4152)
1 parent d54a457 commit b95fdc7

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

scapy/utils.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,8 +3171,8 @@ def whois(ip_address):
31713171

31723172

31733173
class PeriodicSenderThread(threading.Thread):
3174-
def __init__(self, sock, pkt, interval=0.5):
3175-
# type: (Any, _PacketIterable, float) -> None
3174+
def __init__(self, sock, pkt, interval=0.5, ignore_exceptions=True):
3175+
# type: (Any, _PacketIterable, float, bool) -> None
31763176
""" Thread to send packets periodically
31773177
31783178
Args:
@@ -3187,13 +3187,20 @@ def __init__(self, sock, pkt, interval=0.5):
31873187
self._socket = sock
31883188
self._stopped = threading.Event()
31893189
self._interval = interval
3190+
self._ignore_exceptions = ignore_exceptions
31903191
threading.Thread.__init__(self)
31913192

31923193
def run(self):
31933194
# type: () -> None
31943195
while not self._stopped.is_set() and not self._socket.closed:
31953196
for p in self._pkts:
3196-
self._socket.send(p)
3197+
try:
3198+
self._socket.send(p)
3199+
except (OSError, TimeoutError) as e:
3200+
if self._ignore_exceptions:
3201+
return
3202+
else:
3203+
raise e
31973204
self._stopped.wait(timeout=self._interval)
31983205
if self._stopped.is_set() or self._socket.closed:
31993206
break

0 commit comments

Comments
 (0)