Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Remove usage of not standard nc in remote_ops.py is_port_free#284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
dmitry-lipetsk merged 6 commits intomasterfromfix-for-remote
Aug 16, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletionstestgres/operations/local_ops.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -583,6 +583,8 @@ def get_process_children(self, pid):

def is_port_free(self, number: int) -> bool:
assert type(number) == int # noqa: E721
assert number >= 0
assert number <= 65535 # OK?

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
Expand Down
46 changes: 32 additions & 14 deletionstestgres/operations/remote_ops.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@
import logging
import typing
import copy
import re

from ..exceptions import ExecUtilException
from ..exceptions import InvalidOperationException
Expand DownExpand Up@@ -680,23 +681,45 @@ def get_process_children(self, pid):

def is_port_free(self, number: int) -> bool:
assert type(number) == int # noqa: E721
assert number >= 0
assert number <= 65535 # OK?

cmd = ["nc", "-w", "5", "-z", "-v", "localhost", str(number)]
# grep -q returns 0 if a listening socket on that port is found
port_hex = format(number, '04X')

exit_status, output, error = self.exec_command(cmd=cmd, encoding=get_default_encoding(), ignore_errors=True, verbose=True)
# sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt ...
# 137: 0A01A8C0:EC08 1DA2A959:01BB 01 00000000:00000000 02:00000000 00000000 ...
C_REGEXP = r"^\s*[0-9]+:\s*[0-9a-fA-F]{8}:" + re.escape(port_hex) + r"\s+[0-9a-fA-F]{8}:[0-9a-fA-F]{4}\s+"

assert type(output) == str # noqa: E721
assert type(error) == str # noqa: E721
# Search /proc/net/tcp for any entry with this port
# NOTE: grep requires quote string with regular expression
# TODO: added a support for tcp/ip v6
grep_cmd_s = "grep -q -E \"" + C_REGEXP + "\" /proc/net/tcp"

cmd = [
"/bin/bash",
"-c",
grep_cmd_s,
]

exit_status, output, error = self.exec_command(
cmd=cmd,
encoding=get_default_encoding(),
ignore_errors=True,
verbose=True
)

# grep exit 0 -> port is busy
if exit_status == 0:
return__class__._is_port_free__process_0(error)
returnFalse

# grep exit 1 -> port is free
if exit_status == 1:
return __class__._is_port_free__process_1(error)

errMsg = "nc returns an unknown result code: {0}".format(exit_status)
return True

RaiseError.CommandExecutionError(
# any other code is an unexpected error
errMsg = f"grep returned unexpected exit code: {exit_status}"
raise RaiseError.CommandExecutionError(
cmd=cmd,
exit_code=exit_status,
message=errMsg,
Expand DownExpand Up@@ -746,12 +769,7 @@ def _is_port_free__process_0(error: str) -> bool:
@staticmethod
def _is_port_free__process_1(error: str) -> bool:
assert type(error) == str # noqa: E721
#
# Example of error text:
# "nc: connect to localhost (127.0.0.1) port 1024 (tcp) failed: Connection refused\n"
#
# May be here is needed to check error message?
#
return True

@staticmethod
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp