-
Notifications
You must be signed in to change notification settings - Fork 0
/
heartbeat.py
35 lines (30 loc) · 1 KB
/
heartbeat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from cgi import test
import requests
from mcstatus import MinecraftServer
import time
def send_heartbeat(monitor_url, log):
_ = requests.get(monitor_url)
if log:
print('sent heartbeat')
def test_server(server_url, log, port):
try:
server = MinecraftServer(server_url, port)
status = server.status()
if log:
print(f"The server has {status.players.online} players and replied in {status.latency} ms")
return True
except TimeoutError:
if log:
print('server down')
return False
def loop( server_url = 'minecraft.lostinthe.cloud',
port=25565,
monitor_url = 'https://status.lostinthe.cloud/api/push/MVSv7gRI3F?msg=OK&ping=',
delay = 60.0,
log = False):
starttime = time.time()
while True:
if test_server(server_url, log, port):
send_heartbeat(monitor_url, log)
time.sleep(delay - ((time.time() - starttime) % delay))
loop(delay=60, log=True)