Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
87 changes: 87 additions & 0 deletions EmAySee_HostPinger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import subprocess
import platform
import os

class EmAySee_HostPinger:
"""
Pings a specified host and outputs 1 if the host is reachable (up)
and 0 if the host is unreachable (down).
"""
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"host": ("STRING", {"default": "google.com"}), # Hostname or IP address to ping
}
}

RETURN_TYPES = ("INT",) # Output is a single integer (1 or 0)
RETURN_NAMES = ("is_up",) # Name for the output
CATEGORY = "EmAySee_Network" # Category in the ComfyUI menu
TITLE = "EmAySee Host Pinger" # Title displayed on the node

FUNCTION = "EmAySee_check_host" # The method that will be executed

def EmAySee_check_host(self, host):
"""
Executes a system ping command to check if the host is reachable.
Returns 1 if successful, 0 otherwise.
"""
# Determine the correct ping command based on the operating system
param = '-n' if platform.system().lower() == 'windows' else '-c'
count = '1' # Number of ping requests
timeout_param = '-w' if platform.system().lower() == 'windows' else '-W'
timeout_value = '1000' if platform.system().lower() == 'windows' else '1' # Timeout in ms (Windows) or seconds (Linux/macOS)

command = [
'ping',
param, count,
timeout_param, timeout_value,
host
]

is_up = 0 # Default to 0 (down)

try:
# Execute the ping command
# capture_output=True captures stdout and stderr
# text=True decodes stdout/stderr as text
# timeout sets a maximum time to wait for the command to complete
result = subprocess.run(
command,
capture_output=True,
text=True,
timeout=2, # Set a reasonable timeout for the subprocess itself
check=False # Do not raise an exception for non-zero exit codes (like host unreachable)
)

# Check the return code
# A return code of 0 usually indicates success (host is reachable)
if result.returncode == 0:
is_up = 1
else:
# You can optionally print stderr for debugging if the ping fails
# print(f"Ping command failed for {host}: {result.stderr}")
is_up = 0

except FileNotFoundError:
print(f"Error: 'ping' command not found. Make sure ping is installed and in your system's PATH.")
is_up = 0
except subprocess.TimeoutExpired:
print(f"Error: Ping command timed out for {host}.")
is_up = 0
except Exception as e:
print(f"An unexpected error occurred while pinging {host}: {e}")
is_up = 0

return (is_up,) # Return the result as a tuple

# Mapping of node class name to the class
NODE_CLASS_MAPPINGS = {
"EmAySee_HostPinger": EmAySee_HostPinger
}

# Mapping of node class name to the display name in the UI
NODE_DISPLAY_NAME_MAPPINGS = {
"EmAySee_HostPinger": "EmAySee Host Pinger"
}
38 changes: 38 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import importlib
from pathlib import Path

NODE_CLASS_MAPPINGS = {}
NODE_DISPLAY_NAME_MAPPINGS = {}

# Define the author
__author__ = "EmAySee"

# Get a list of all Python files in subdirectories, using pathlib
module_paths = []
for root, _, files in os.walk(Path(__file__).parent):
for file in files:
if file.endswith(".py") and file != "__init__.py":
module_paths.append(Path(root) / file)

for module_path in module_paths:
try:
# Construct the module spec. This is the critical part for subfolders.
relative_path = module_path.relative_to(Path(__file__).parent)
module_name = str(relative_path).replace(os.sep, ".")[:-3] # Convert path to dot notation
module = importlib.import_module(f".{module_name}", package=__package__)

# Check for the required dictionaries and merge them
if hasattr(module, "NODE_CLASS_MAPPINGS") and hasattr(module, "NODE_DISPLAY_NAME_MAPPINGS"):
NODE_CLASS_MAPPINGS.update(module.NODE_CLASS_MAPPINGS)
NODE_DISPLAY_NAME_MAPPINGS.update(module.NODE_DISPLAY_NAME_MAPPINGS)
print(f"Loaded nodes from {module_name}")
else:
print(f"Warning: {module_name} does not contain NODE_CLASS_MAPPINGS or NODE_DISPLAY_NAME_MAPPINGS. Skipping.")

except ImportError as e:
print(f"Error importing {module_name}: {e}")
except Exception as e:
print(f"An unexpected error occurred while processing {module_name}: {e}")

__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"]
14 changes: 14 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[project]
name = "emaysee_customnodes"
description = ""
version = "1.0.0"
license = {file = "LICENSE"}

[project.urls]
Repository = "https://github.com/EmAySee/ComfyUI_EmAySee_CustomNodes"
# Used by Comfy Registry https://comfyregistry.org

[tool.comfy]
PublisherId = ""
DisplayName = "ComfyUI_EmAySee_CustomNodes"
Icon = ""