-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
executable file
·56 lines (47 loc) · 1.53 KB
/
client.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
from mcastservice import McastServiceClient
import sys,re,socket
from misc import readConf
class OnlineCalcClient(McastServiceClient):
def run(self,port,request):
exp = "^[0-9\.()\+\-\/\*]*$"
if re.match(exp,request):
request = "%s:%s"%(port,request)
return McastServiceClient.run(self,request)[0]
else:
return None
def usage():
print "%s mcastPort mcastAddr udpPort udpAddr" % sys.argv[0]
print "\tUm prompt informa que os calculos ja podem ser digitados."
print "\tUm <enter> separa cada calculo."
def main(argc, argv):
if argc > 2:
usage()
sys.exit(1)
# Argumentos
opts = readConf()
mcastPort = int(opts["mcast_port"])
mcastAddr = opts["mcast_addr"]
udpPort = int(opts["udp_port"])
udpAddr = len(sys.argv)>1 and sys.argv[1] or socket.gethostname()
# Cria conexao com o multicast e abre udp para resposta
onlinecalc = OnlineCalcClient(mcastPort,mcastAddr,udpPort,udpAddr)
# Loop para leitura de comandos
print "ctrl+c para sair."
while True:
try:
calc = raw_input("> ")
if calc:
try:
print onlinecalc.run(udpPort,calc)
except socket.timeout:
print "Sem resposta (timeout estourou)"
except (EOFError,KeyboardInterrupt):
print ""
break
return 0
if __name__ == "__main__":
try:
sys.exit(main(len(sys.argv),sys.argv))
except:
raise