Created
May 10, 2018 13:29
-
-
Save mateusz-lisik/397944c52dd9fe4478b76917cac0372a to your computer and use it in GitHub Desktop.
Simple Python socket monitoring script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import socket | |
from datetime import datetime | |
from time import sleep | |
is_connected = None | |
if len(sys.argv) != 3: | |
print "Provide host and port as parameters" | |
sys.exit(1) | |
host = sys.argv[1] | |
port = sys.argv[2] | |
print "Monitoring host: " + host + " port: " + port | |
while True: | |
# new_state = None | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.settimeout(2.0) | |
try: | |
s.connect((host, int(port))) | |
new_state = True | |
s.close() | |
except IOError: | |
new_state = False | |
if new_state != is_connected: | |
is_connected = new_state | |
print str(datetime.now()) + ' connection status: ' + str(is_connected) | |
sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment