Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/lib/utils/timeout.py
2989 views
1
#!/usr/bin/env python
2
3
"""
4
Copyright (c) 2006-2025 sqlmap developers (https://sqlmap.org)
5
See the file 'LICENSE' for copying permission
6
"""
7
8
import threading
9
10
from lib.core.data import logger
11
from lib.core.enums import CUSTOM_LOGGING
12
from lib.core.enums import TIMEOUT_STATE
13
14
def timeout(func, args=None, kwargs=None, duration=1, default=None):
15
class InterruptableThread(threading.Thread):
16
def __init__(self):
17
threading.Thread.__init__(self)
18
self.result = None
19
self.timeout_state = None
20
21
def run(self):
22
try:
23
self.result = func(*(args or ()), **(kwargs or {}))
24
self.timeout_state = TIMEOUT_STATE.NORMAL
25
except Exception as ex:
26
logger.log(CUSTOM_LOGGING.TRAFFIC_IN, ex)
27
self.result = default
28
self.timeout_state = TIMEOUT_STATE.EXCEPTION
29
30
thread = InterruptableThread()
31
thread.start()
32
thread.join(duration)
33
34
if thread.is_alive():
35
return default, TIMEOUT_STATE.TIMEOUT
36
else:
37
return thread.result, thread.timeout_state
38
39