Skip to content

Commit 8fd0539

Browse files
author
vshepard
committed
Remove usage of not standard nc in remote_ops.py is_port_free
1 parent 79bc692 commit 8fd0539

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

testgres/operations/remote_ops.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import logging
1111
import typing
1212
import copy
13+
import socket
1314

1415
from ..exceptions import ExecUtilException
1516
from ..exceptions import InvalidOperationException
@@ -681,28 +682,24 @@ def get_process_children(self, pid):
681682
def is_port_free(self, number: int) -> bool:
682683
assert type(number) == int # noqa: E721
683684

684-
cmd = ["nc", "-w", "5", "-z", "-v", "localhost", str(number)]
685+
host = 'localhost'
686+
port = number
687+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
688+
sock.settimeout(5.0)
685689

686-
exit_status, output, error = self.exec_command(cmd=cmd, encoding=get_default_encoding(), ignore_errors=True, verbose=True)
687-
688-
assert type(output) == str # noqa: E721
689-
assert type(error) == str # noqa: E721
690-
691-
if exit_status == 0:
692-
return __class__._is_port_free__process_0(error)
693-
694-
if exit_status == 1:
695-
return __class__._is_port_free__process_1(error)
696-
697-
errMsg = "nc returns an unknown result code: {0}".format(exit_status)
698-
699-
RaiseError.CommandExecutionError(
700-
cmd=cmd,
701-
exit_code=exit_status,
702-
message=errMsg,
703-
error=error,
704-
out=output
705-
)
690+
try:
691+
result = sock.connect_ex((host, port))
692+
return result != 0
693+
except Exception as e:
694+
raise RaiseError.CommandExecutionError(
695+
cmd=['socket.connect_ex', host, str(port)],
696+
exit_code=getattr(e, 'errno', None),
697+
message=f"Error checking port {port}: {e}",
698+
error=str(e),
699+
out=""
700+
)
701+
finally:
702+
sock.close()
706703

707704
def get_tempdir(self) -> str:
708705
command = ["mktemp", "-u", "-d"]
@@ -746,12 +743,7 @@ def _is_port_free__process_0(error: str) -> bool:
746743
@staticmethod
747744
def _is_port_free__process_1(error: str) -> bool:
748745
assert type(error) == str # noqa: E721
749-
#
750-
# Example of error text:
751-
# "nc: connect to localhost (127.0.0.1) port 1024 (tcp) failed: Connection refused\n"
752-
#
753746
# May be here is needed to check error message?
754-
#
755747
return True
756748

757749
@staticmethod

0 commit comments

Comments
 (0)