Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Commit 81ec781

Browse files
mauricesvpMMunier
andauthored
Connect-retries; add makefile diff (#71)
* makefile diff & connect-retries * make diff * version bump Co-authored-by: M:Munier <[email protected]>
1 parent 864d4ae commit 81ec781

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ lint:
44
python3 -m flake8 --select F --per-file-ignores="__init__.py:F401" src/ tests/ example/
55
python3 -m mypy src/ tests/ example/
66

7+
diff:
8+
python3 -m isort --diff -rc src/ tests/ example/
9+
python3 -m black --diff src/ tests/ example/
10+
711
format:
812
python3 -m isort -rc src/ tests/ example/
913
python3 -m black src/ tests/ example/

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setuptools.setup(
1111
name="enochecker",
12-
version="0.2.1",
12+
version="0.2.2",
1313
author="domenukk",
1414
author_email="[email protected]",
1515
description="Library to build checker scripts for EnoEngine A/D CTF Framework in Python",

src/enochecker/enochecker.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from .checkerservice import CHECKER_METHODS, init_service
3131
from .logging import ELKFormatter
3232
from .nosqldict import NoSqlDict
33-
from .results import CheckerResult, EnoException, Result
33+
from .results import CheckerResult, EnoException, OfflineException, Result
3434
from .storeddict import DB_DEFAULT_DIR, DB_GLOBAL_CACHE_SETTING, StoredDict
3535
from .useragents import random_useragent
3636
from .utils import SimpleSocket, snake_caseify
@@ -739,6 +739,7 @@ def connect(
739739
host: Optional[str] = None,
740740
port: Optional[int] = None,
741741
timeout: Optional[int] = None,
742+
retries: int = 3,
742743
) -> SimpleSocket:
743744
"""
744745
Open a socket/telnet connection to the remote host.
@@ -750,22 +751,51 @@ def connect(
750751
:param timeout: timeout on connection (defaults to self.timeout)
751752
:return: A connected Telnet instance
752753
"""
754+
753755
if timeout:
754-
timeout_fun: Callable[[], int] = lambda: cast(int, timeout)
756+
757+
def timeout_fun() -> int:
758+
return cast(int, timeout)
759+
755760
else:
756-
timeout = self.time_remaining // 2
757-
timeout_fun = lambda: self.time_remaining // 2
761+
762+
def timeout_fun() -> int:
763+
return self.time_remaining // 2
758764

759765
if port is None:
760766
port = self.port
761767
if host is None:
762768
host = self.address
763-
self.debug(
764-
"Opening socket to {}:{} (timeout {} secs).".format(host, port, timeout)
765-
)
766-
return SimpleSocket(
767-
host, port, timeout=timeout, logger=self.logger, timeout_fun=timeout_fun
768-
)
769+
770+
if retries < 0:
771+
raise ValueError("Number of retries must be greater than zero.")
772+
773+
for i in range(0, retries + 1): # + 1 for the initial try
774+
try:
775+
776+
timeout = timeout_fun()
777+
self.debug(
778+
"Opening socket to {}:{} (timeout {} secs).".format(
779+
host, port, timeout
780+
)
781+
)
782+
return SimpleSocket(
783+
host,
784+
port,
785+
timeout=timeout,
786+
logger=self.logger,
787+
timeout_fun=timeout_fun,
788+
)
789+
790+
except Exception as e:
791+
self.warning(
792+
f"Failed to establish connection to {host}:{port}, Try #{i+1} of {retries+1} ",
793+
exc_info=e,
794+
)
795+
continue
796+
797+
self.error(f"Failed to establish connection to {host}:{port}")
798+
raise OfflineException("Failed establishing connection to service.")
769799

770800
@property
771801
def http_useragent(self) -> str:

0 commit comments

Comments
 (0)