Docs - Examples - ping
This example is an improvised ping
utility for ATEM switchers:
$ python3 ping.py -h
[Tue Nov 24 21:48:32 2020] PyATEMMax demo script: ping
usage: ping.py [-h] [-i INTERVAL] ip
positional arguments:
ip switcher IP address
optional arguments:
-h, --help show this help message and exit
-i INTERVAL, --interval INTERVAL
wait INTERVAL seconds between pings, default: 1.0
It tries to connect to the specified ip once a second and reports result. Also allows to specify the interval between pings
with the -i
parameter.
$ python3 ping.py 192.168.1.111
[Tue Nov 24 21:52:14 2020] PyATEMMax demo script: ping
[Tue Nov 24 21:52:14 2020] Pinging ATEM switcher at 192.168.1.111 every 1.0 seconds
[Tue Nov 24 21:52:14 2020] Switcher connected
[Tue Nov 24 21:52:15 2020] Switcher connected
[Tue Nov 24 21:52:16 2020] Switcher connected
...
$ python3 ping.py 192.168.1.222 -i 10
[Tue Nov 24 21:52:26 2020] PyATEMMax demo script: ping
[Tue Nov 24 21:52:26 2020] Pinging ATEM switcher at 192.168.1.222 every 10.0 seconds
[Tue Nov 24 21:52:26 2020] Switcher DISCONNECTED
[Tue Nov 24 21:52:36 2020] Switcher DISCONNECTED
[Tue Nov 24 21:52:47 2020] Switcher DISCONNECTED
...
Code walkthrough
Start with the usual initial steps (explained in Examples)
#!/usr/bin/env python3
# coding: utf-8
"""ping.py - PyATEMMax demo script.
Part of the PyATEMMax library."""
import argparse
import time
import PyATEMMax
print(f"[{time.ctime()}] PyATEMMax demo script: ping")
DEFAULT_INTERVAL = 1.0
parser = argparse.ArgumentParser()
parser.add_argument('ip', help='switcher IP address')
parser.add_argument('-i', '--interval',
help=f'wait INTERVAL seconds between pings, default: {DEFAULT_INTERVAL}',
default=DEFAULT_INTERVAL,
type=float)
args = parser.parse_args()
print(f"[{time.ctime()}] Pinging ATEM switcher at {args.ip} every {args.interval} seconds")
Start working with the switcher:
First, the ATEMMax
object is created:
switcher = PyATEMMax.ATEMMax()
Then, the script enters an infinite loop:
while True:
For each iteration, the script will:
- Ping the switcher using the provided ip address
switcher.ping(args.ip)
- Wait for the handshake to finish.
if switcher.waitForConnection():
- With the result of
waitForConnection
the script knows which message to print:
print(f"[{time.ctime()}] Switcher connected")
else:
print(f"[{time.ctime()}] Switcher DISCONNECTED")
- Then, the connection to the switcher is closed:
switcher.disconnect()
- And the script sleeps for the speciried
interval
time.sleep(args.interval)
Stripped down version
import time
import PyATEMMax
switcher = PyATEMMax.ATEMMax()
while True:
switcher.ping("192.168.1.111")
if switcher.waitForConnection():
print(f"Switcher connected")
else:
print(f"Switcher DISCONNECTED")
switcher.disconnect()
time.sleep(1)