def cliThread(): print ('Entrando em CLI...') command = '' while command != 'exit': command = raw_input('>>> ') if command == 'clients': for i in clients: print (i) else: print (command+" nao reconhecido...") def server(server, port, debug = False): BUFFER_SIZE = 1024 maxconnections = 10 print ('Criando socket do servidor...') sck = socket (AF_INET, SOCK_STREAM) sck.bind ( (server, port) ) sck.listen (maxconnections) Thread (target=cliThread, args=()).start() print ('Estado: aguardando conexoes...') while True: auxSck = sck.accept () clients.append (auxSck) a=Thread (target = listernerController, args=(auxSck,),) a.start() print ('Servidor encerrado.') terminal.cout ('warning', "<server ip><port to listen>") if len (argv) >= 2: server ( argv[1], int(argv[2]), True )
return (divisores) def isPerfect (n): divisores = getDivisores (n) sigma = 0 if debug: term.cout ('white', 'Divisors: ') for i in divisores: term.cout ('warning', str (i)) if len (divisores) > 0: for i in divisores: sigma += i if sigma == n: return (True) return (False) n = int (raw_input ("Entre com o numero>> ")) while n != -1: if (isPerfect (n)): term.cout ('warning', str (n) + ' [perfect]') else: term.cout ('error', str (n) + ' [imperfect]') n = int (raw_input ("Entre com o numero>> "))
buff = serversocket.recv (BUFFER_SIZE) print (buff) def client (server, port, debug = False): if debug: print ('Criando socket de cliente...') sck = socket (AF_INET, SOCK_STREAM) if debug: print ("\t\tOK") print ('Connectando cliente ao servidor ('+server+', '+str(port)+')' ) sck.connect ( (server, port) ) sck.send (sck.getsockname()[0]) Thread (target=listernerController, args=(sck,)).start () buff = True mode = 'message' while buff != 'exit': buff = raw_input (mode+":") aux = toArray (buff, ':') if aux[0] == '/switch': mode = aux[1] elif mode == 'message': sck.send (mode+':'+buff) elif mode == 'command': sck.send (mode+':'+buff) if debug: print ('Encerrando cliente...') if len (argv) < 2: terminal.cout ('warning', '<ip do servidor><porta de comunicacao>') else: client (argv[1], int(argv[2]), True )