#!/usr/bin/python3
import socket
import sys
from getopt import getopt

options, args = getopt(sys.argv[1:], "h:p:")

HOST = "localhost"
PORT = 9999

for o,a in options:
    if o == "-h":
        HOST = a
    elif o == "-p":
        PORT = int(a)
    else:
        print("unrecognized argument")
        exit(2)
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    s.sendall(b"status\n")
    answer = s.recv(50).decode("ascii")
    s.close()
    if answer.strip() == "Running":
        print("OK: received 'Running'")
        exit(0)
    elif answer.strip() == "Not running":
        print("ERROR: received 'Not Running'")
        exit(2)
    else:
        print("ERROR: received '"+answer.strip()+"'")
        exit(2)
except OSError as err:
    print("ERROR: Network Error")
    print(err)
    exit(2)
