sqlmap/lib/utils/timeout.py

38 lines
1.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2010-04-06 18:33:57 +04:00
2010-04-07 00:40:14 +04:00
"""
2019-01-05 23:38:52 +03:00
Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
2010-04-07 00:40:14 +04:00
"""
2010-04-06 18:33:57 +04:00
import threading
2010-04-06 19:24:56 +04:00
from lib.core.data import logger
2012-12-07 14:54:34 +04:00
from lib.core.enums import CUSTOM_LOGGING
from lib.core.enums import TIMEOUT_STATE
2010-04-06 19:24:56 +04:00
2010-04-06 18:59:31 +04:00
def timeout(func, args=(), kwargs={}, duration=1, default=None):
2010-04-06 18:33:57 +04:00
class InterruptableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.result = None
self.timeout_state = None
2010-04-06 18:33:57 +04:00
def run(self):
try:
self.result = func(*args, **kwargs)
self.timeout_state = TIMEOUT_STATE.NORMAL
2010-04-06 19:24:56 +04:00
except Exception, msg:
2012-12-07 14:54:34 +04:00
logger.log(CUSTOM_LOGGING.TRAFFIC_IN, msg)
2010-04-06 18:33:57 +04:00
self.result = default
self.timeout_state = TIMEOUT_STATE.EXCEPTION
2010-04-06 18:33:57 +04:00
thread = InterruptableThread()
thread.start()
2010-04-06 18:59:31 +04:00
thread.join(duration)
2010-04-07 00:40:14 +04:00
2010-04-06 19:28:34 +04:00
if thread.isAlive():
return default, TIMEOUT_STATE.TIMEOUT
2010-04-06 18:33:57 +04:00
else:
return thread.result, thread.timeout_state