Miscellaneous Interfaces
Ping
- class shc.interfaces.ping.Ping(address: str, interval: timedelta = datetime.timedelta(seconds=300), number: int = 5, timeout: float = 1.0)
A simple Subscribable object that periodically checks a given network host to be alive, using the ping command, and publishes the result as a bool value.
This can be used for a simple presence monitoring, e.g. by pinging your mobile phone in your local WiFi network.
- Parameters:
address – The network host to be checked. Can be any string that can be passed as the host argument to the ping command, i.e. an IPv4 address, an IPv6 address, a host name of a full-qualified domain name.
interval – The interval in which the host state shall be checked by pinging it
number – The number of ECHO requests to send each time. Passed to ping via the -c argument (resp. -n on Windows). The host is considered to be alive (= True is published) if any of the sent pings is successfully received.
timeout – The timeout for each individual ECHO request in seconds, passed to ping via the -W argument (resp. -w on Windows)
Command
These interfaces/objects allow to call a subprocess and use the result in SHC:
- class shc.interfaces.command.Command(command: Union[str, List[str]], shell=False, include_std_err=False, check=False)
A Readable object that executes a (fixed) given command returns the result when being read.
This readable object can easily be combined with the
shc.misc.PeriodicReader
to periodically publish the command’s output:# Publish "Hello, world!" every 5seconds (the hard way ;) ) PeriodicReader(Command(['echo', 'Hello, world!']), datetime.timedelta(seconds=5))
The parameters are similar to subprocess.run()’s:
- Parameters:
command – The command to execute incl. arguments and parameters. A single str, when shell is True, otherwise it should be list of the command and its individual command line arguments.
shell – If True, the command will be within a shell, otherwise it will be started as a plain subprocess
include_std_err – If True, stderr output from the command will be included in the output. This is similar to adding ‘2>&1’ to your shell command
check – If True, the exit code of the command will be checked the read() method raises an UninitializedError instead of returning the output of the command in case of a non-zero exit code.
- class shc.interfaces.command.CommandExitCode(command: Union[str, List[str]], shell=False)
A Readable object that executes a (fixed) given command returns the its exit code when being read.
This readable object can easily be combined with the
shc.misc.PeriodicReader
to periodically publish the command’s exit code:# Publish 1 every 5seconds (the hard way ;) ) PeriodicReader(Command(['false']), datetime.timedelta(seconds=5))
The parameters are similar to subprocess.run()’s:
- Parameters:
command – The command to execute incl. arguments and parameters. A single str, when shell is True, otherwise it should be list of the command and its individual command line arguments.
shell – If True, the command will be within a shell, otherwise it will be started as a plain subprocess