|
10 | 10 | importlogging
|
11 | 11 | importtyping
|
12 | 12 | importcopy
|
| 13 | +importsocket |
13 | 14 |
|
14 | 15 | from ..exceptionsimportExecUtilException
|
15 | 16 | from ..exceptionsimportInvalidOperationException
|
@@ -681,28 +682,24 @@ def get_process_children(self, pid):
|
681 | 682 | defis_port_free(self,number:int)->bool:
|
682 | 683 | asserttype(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 |
| -asserttype(output)==str# noqa: E721 |
689 |
| -asserttype(error)==str# noqa: E721 |
690 |
| - |
691 |
| -ifexit_status==0: |
692 |
| -return__class__._is_port_free__process_0(error) |
693 |
| - |
694 |
| -ifexit_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 | +returnresult!=0 |
| 693 | +exceptExceptionase: |
| 694 | +raiseRaiseError.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 | defget_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 | asserttype(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 | returnTrue
|
756 | 748 |
|
757 | 749 | @staticmethod
|
|