-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor_process.py
More file actions
122 lines (111 loc) · 4.26 KB
/
monitor_process.py
File metadata and controls
122 lines (111 loc) · 4.26 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
"""
Monitor Process
"""
import socket
import time
import re
import sys
port = 50000
size = 1024
'''
Function : command_help
Input : None
Definition : List of commands supported
'''
def command_help():
print ('monitor <hostname> : To connect to Monitor')
print ('get : To get the current simulator value')
print ('set <value> : To set the current simulator value')
print ('stop : To stop the simulator running')
print ('continue : To continue the simualtor running')
print ('quit : To quit from Monitor')
'''
Function : is_valid_hostname
Input : hostname
Definition : Verifies if hostname provided by user is valid
'''
def is_valid_hostname(hostname):
if len(hostname) > 255:
return False
if hostname[-1] == ".":
hostname = hostname[:-1] # strip exactly one dot from the right, if present
allowed = re.compile("(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
return all(allowed.match(x) for x in hostname.split("."))
print (' This is the Monitor Process ')
command_help()
connected = False
while True:
print ('Please Enter Command to be executed')
line = input()
if not line:
continue
args = line.split()
if args[0] == 'get':
if len(args) > 1:
print ('Get command cannot have more arguments')
command_help()
continue
elif args[0] == 'set':
if len(args) != 2:
print ('set command not used properly')
command_help()
continue
try:
val = int(args[1])
except ValueError:
print("Please pass an integer")
continue
elif args[0] == 'stop':
if len(args) > 1:
print ('stop command not used properly')
command_help()
continue
elif args[0] == 'continue':
if len(args) > 1:
print ('continue command not used properly')
command_help()
continue
elif args[0] == 'quit':
if len(args) > 1:
print ('continue command not used properly')
command_help()
continue
break
elif args[0] == 'monitor':
print ('In monitor ....')
if connected:
print (' Already connected to a Server, you cannot connect to 2 servers at a time')
continue
if len(args) != 2:
print ('monitor command not used properly')
command_help()
continue
hostname = args[1]
if is_valid_hostname(hostname):
host = hostname
try:
#Connect to the required Host using Socket
#SOCK_STREAM we want a TCP Socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
connected = True
except socket.error:
if s:
s.close()
print ("Could not open socket: ")
sys.exit(1)
else:
print ('Invalid hostname')
else:
print ('Unknown command, command not supported')
command_help()
continue
if connected :
s.send(line.encode('ascii'))
resp = s.recv(size)
data = resp.decode('ascii')
print ('Received:', data)
else :
print ('Not connected to any Server please connect before running any other command')
s.close()