|
10 | 10 | import logging
|
11 | 11 | import typing
|
12 | 12 | import copy
|
| 13 | +import socket |
13 | 14 |
|
14 | 15 | from ..exceptions import ExecUtilException
|
15 | 16 | from ..exceptions import InvalidOperationException
|
@@ -681,28 +682,24 @@ def get_process_children(self, pid):
|
681 | 682 | def is_port_free(self, number: int) -> bool:
|
682 | 683 | assert type(number) == int # noqa: E721
|
683 | 684 |
|
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) |
685 | 689 |
|
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() |
706 | 703 |
|
707 | 704 | def get_tempdir(self) -> str:
|
708 | 705 | command = ["mktemp", "-u", "-d"]
|
@@ -746,12 +743,7 @@ def _is_port_free__process_0(error: str) -> bool:
|
746 | 743 | @staticmethod
|
747 | 744 | def _is_port_free__process_1(error: str) -> bool:
|
748 | 745 | 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 |
| - # |
753 | 746 | # May be here is needed to check error message?
|
754 |
| - # |
755 | 747 | return True
|
756 | 748 |
|
757 | 749 | @staticmethod
|
|
0 commit comments