Process Control#

Process launching and querying

class mhi.common.process.AddressPortPid(address, port, pid)#
address: str#

Alias for field number 0

port: int#

Alias for field number 1

pid: int#

Alias for field number 2

class mhi.common.process.AddressPortPidApp(address, port, pid, appname)#
address: str#

Alias for field number 0

port: int#

Alias for field number 1

pid: int#

Alias for field number 2

appname: str#

Alias for field number 3

class mhi.common.process.MIB_TCPROW_LH#
class mhi.common.process.MIB_TCPROW_OWNER_PID#
class mhi.common.process.MIB_TCP6ROW_OWNER_PID#
mhi.common.process.process_pids(*names: str) List[Tuple[str, int]]#

Return the process id’s of any process with the given executable names.

Application names may include the % wildcard. For example, the following query might find both SkypeApp.exe and SkypeBackgroundHost.exe:

process_ids('skype%.exe')

Since applications may terminate and can be started at any time, the returned value is obsolete immediately upon being returned.

Parameters:

*names (str) – application filename patterns, without any path.

Returns:

A list of process name & process id pairs

Return type:

List[tuple]

mhi.common.process.is_running(app_name: str) bool#

Determine if there is an app process in the list of running processes.

Application names may include the % wildcard. For example, the following query might find both SkypeApp.exe and SkypeBackgroundHost.exe:

is_running('skype%.exe')

Since applications may terminate and can be started at any time, the returned value is obsolete immediately upon being returned.

Parameters:

app (str) – application filename, without any path.

Returns:

True if a process can be found, False otherwise.

Return type:

bool

mhi.common.process.host_addresses(host: str) Set[str]#

Convert a host name into one or more IP address (ie, IPv4 & IPv6)

mhi.common.process.is_local_host(host: str) bool#

Does the given hostname represent the local host?

Returns True if the given ‘host’ is a public IP address of the host, a loopback address, an IPADDR_ANY-type address, or a symbolic name that represents any of these.

mhi.common.process.host_filter(host: str)#

Return a filter function to filter an iterable of AddressPortPid or AddressPortPidApp listeners against the given host

mhi.common.process.tcp_ports_in_use() Set[int]#

Find all TCP ports in use

Returns:

Set of all ports in use by the TCP protocol

Return type:

Set[int]

mhi.common.process.unused_tcp_port(ports: range | None = None) int#

Find an available TCP ports in the specified range. If no range is given, the dynamic/private range (0xC000–0xFFFF) is used.

Returns:

an available TCP port

Return type:

int

mhi.common.process.listener_ports_by_pid(*pid: int) List[AddressPortPid]#

Find all listener ports opened by processes with the given PIDs.

Since applications may terminate and can be started at any time, as well as open and close ports at any time, the returned value is obsolete immediately upon being returned.

Parameters:

*pid (int) – Process ids

Returns:

a list of (addr, port, pid) tuples

Return type:

List[tuple]

mhi.common.process.listener_ports_by_name(*names: str) List[AddressPortPidApp]#

Find all listener ports opened by processes with the given executable name.

Application names may include the % wildcard. For example, the following query might find both listener ports opened by both SkypeApp.exe and SkypeBackgroundHost.exe:

listener_ports_by_name('skype%.exe')

Since applications may terminate and can be started at any time, as well as open and close ports at any time, the returned value is obsolete immediately upon being returned.

Parameters:

*names (str) – application filename patterns, without any path.

Returns:

a list of (addr, port, pid, name) tuples

Return type:

List[tuple]

mhi.common.process.versions(app_name: str) List[Tuple[str, bool]]#

Find the installed versions of an MHI application.

Returns:

List of tuples of version and bit-size

Return type:

List[Tuple]

mhi.common.process.find_exe(app_name: str, version: str | None = None, x64: bool | None = None, minimum: str | None = None, maximum: str | None = None, allow_alpha: bool = False, allow_beta: bool = False) str | None#

Find an MHI application executable.

If no version is specified, the highest version available is used, with Alpha and Beta versions being considered the highest and second highest respectively. If no x64 flag is given, a 32-bit or 64-bit version may be returned, with preference given to 64-bit versions.

Parameters:
  • app (str) – name of the application (without any extension)

  • version (str) – application version such as ‘5.0.1’ or ‘5.1 (Beta)’

  • x64 (bool) – True for 64-bit version, False for 32-bit version

  • minimum (str) – The lowest allowable version, such as ‘5.0’

  • maximum (str) – The highest allowable version, such as ‘5.0.9’

Returns:

the path to the executable

Return type:

str

Added in version 2.1: minimum, maximum, allow_alpha & allow_beta parameters.

Changed in version 2.4.5: allow_alpha, allow_beta parameters are no longer supported.

Changed in version 3.0.3: allow_beta parameter is once again supported.

mhi.common.process.launch(*args: str, options: Dict[str, Any] | None = None, **kwargs)#

Launch an application process.

All {keyword} format codes in the list of args strings are replaced by the value in the corresponding options dictionary and/or kwargs key-value argument pairs.

For example:

launch("C:\{dir}\{name}.exe", "/silent:{silent}", "/title:{title!r}",
       dir="temp", name="app", silent=True, title="Hello world")

will launch C:\temp\app.exe passing the arguments /silent:True and /title:'Hello world'

Parameters:
  • *args (str) – the application and the arguments for the application

  • options – values which may be substituted in the application arguments

  • **kwargs – additional substitution values

Returns:

The subprocess handle

Special keyword arguments#

Keyword=Value

Effect

minimize=True

process started with SW_SHOWMINNOACTIVE

minimize=False

process started with SW_SHOWNOACTIVATE

debug=True

process is not started, command line printed

mhi.common.process.report(installed=True, minimums=False, running=True, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)#

Report known, installed MHI products