def set_up(node, data): """Transfer configuration-script on instances and run it.""" user = data['username'] ip = node[1] key_file = data['ssh-key'] client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname=ip, username=user, key_filename=key_file) with SCPClient(client.get_transport()) as scp: scp.put('cloud-configuration.sh', "/home/" + user) scp.put('server.py', "/home/" + user) scp.put('clopper_pb2.py', "/home/" + user) scp.put('clopper_pb2_grpc.py', "/home/" + user) scp.put(data['credentials'], "/home/" + user) logging.info("Start installation on " + node[0]) (stdin, stdout, stderr) = client.exec_command('bash ~/cloud-configuration.sh') counter = 0 while not stdout.channel.exit_status_ready(): time.sleep(1) counter += 1 if counter % 60 == 0: logging.info("Installation in progress on " + node[0]) exit_status = stdout.channel.recv_exit_status() if exit_status == 0: logging.info("Installation on " + node[0] + " completed.") else: logging.error("Installation failed on " + node[0]) client.close()
def main(): # start local server to respond to pings and whatnot thrd = threading.Thread(target=server.loop, args=(protocol.node.handler,)) thrd.daemon = True # to ensure program exits even if thrd still runs thrd.start() while 1: # look for things the server might want to know local_changes = statistics.gather() print 'Local changes:',local_changes node_changes = watcher.gather() print 'Node changes:',node_changes # and send them to the server try: s = client.connect(MASTER_ADDR) # 1: data push request = protocol.master.push_request(local_changes, node_changes) stamp,version,do_clear_cache = protocol.master.push_response( client.ask(s, request)) print 'Data pushed to server.' if do_clear_cache: print 'Cache cleared due to server request.' statistics.cache = {} watcher.cache = {} # check if we're out of date if version>conf.VERSION: print 'Client of out date!' print 'Version %d, latest is %d.' % (conf.VERSION, version) print 'Exiting.' import sys sys.exit(1) # 2: node list request print 'Node timestamps, server:',stamp,'mine:',neighbours.stamp if stamp!=neighbours.stamp: # request update! request = protocol.master.nodes_request() stamp,nodes = protocol.master.nodes_response( client.ask(s, request)) print 'Received',len(nodes),' new nodes with stamp:',stamp neighbours.set(stamp,nodes) client.close(s) except: import traceback traceback.print_exc() print 'Failed to connect to server. Aborting communication.' # since the server didn't get our date (and is probably down!) # we really should clear our cache, to ensure the server gets a # full picture statistics.cache = {} watcher.cache = {} # then wait, to not waste resourses print 'Sleeping...' try: time.sleep(POLLING_INTERVAL) except KeyboardInterrupt: print 'Exiting.' return 0
def handle(self, client): while True: try: message = model.Message(**json.loads(self.receive(client))) except (ConnectionAbortedError, ConnectionResetError): print(CONNECTION_ABORTED) return if message.quit: client.close() self.clients.remove(client) return if SHUTDOWN_MESSAGE.lower() == message.message.lower(): self.exit() return self.check(message) if self.first_client.name == message.username: self.first_client.last_message = message.message else: self.second_client.last_message = message.message print(str(self.timetable)) print(str(self.first_client.name)) print(str(self.first_client.last_message)) print(str(self.second_client.name)) print(str(self.second_client.last_message)) self.broadcast(message) self.count += 1 if self.count == 2: self.decide() if self.day != 5: self.start_new_day() else: self.finish()
def listen(self): self.sock.listen(1) while True: try: client, address = self.sock.accept() except OSError: print(CONNECTION_ABORTED) return print(CONNECTED_PATTERN.format(*address)) if len(self.clients) < 2: self.clients.add(client) else: self.server_message.message = messages.SERVER_OVERFLOW client.sendall(self.server_message.marshal()) client.close() threading.Thread(target=self.handle, args=(client,)).start()
def distribute_test_suite(node_dict, test_suite, data): """SSH into the remote instance and transfer data with SCP.""" # compress project-dir project = shutil.make_archive(expanduser('~/tmp/project'), 'gztar', root_dir=data['project']) writer = Writer(data, test_suite.content) #distribute test suite among instances for node, bundle in zip(node_dict.iteritems(), test_suite): config, cl = writer.generate_input(bundle) ip = node[1] key_file = data['ssh-key'] user = data['username'] client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname=ip, username=user, key_filename=key_file) with SCPClient(client.get_transport()) as scp: scp.put(config, "/home/" + user + "/tmp/config.tar.gz") scp.put(cl, "/home/" + user + "/tmp/params.tar.gz") scp.put(project, "/home/" + user + "/tmp/project.tar.gz") client.close()
def client_thread(client, username): print("Running client_thread") client.send(bytes(room_name, "utf-8")) for message in messages: print(message) client.send(bytes(message, "utf-8")) print("Type of username: "******"", username.decode("utf-8") + " has entered the chat \n") while True: try: message = client.recv(2000).decode("utf-8") print(message) print("message received") if message: if message[0:3] == "***": print("command received") action_handler(message[3:]) elif username not in mute_list: print("broadcasting message") broadcast_message(username.decode("utf-8"), message) else: print("poo poo") else: print("Client closed:(") client.close() remove(username) except: continue
def handle(self, client): while True: try: message = model.Message(**json.loads(self.receive(client))) except (ConnectionAbortedError, ConnectionResetError): print(CONNECTION_ABORTED) return if message.quit: client.close() self.clients.remove(client) if (self.game_state.leader.name == message.username and self.game_state.gamer.name is None) \ or (self.game_state.gamer.name == message.username and self.game_state.leader.name is None): self.game_state.gamer.name = None self.game_state.leader.name = None elif self.game_state.leader.name == message.username: self.game_state.leader.name = self.game_state.gamer.name self.game_state.gamer.name = None self.server_message.message = LOGOUT_PATTERN.format(username='******') elif self.game_state.gamer.name == message.username: self.game_state.gamer.name = None self.server_message.message = LOGOUT_PATTERN.format(username='******') self.broadcast(self.server_message) if self.game_state.leader.name is not None: self.start_game() self.game = 0 return print(str(message)) if SHUTDOWN_MESSAGE.lower() == message.message.lower(): self.exit() if self.game_state.leader.name == message.username: self.game_state.leader.name = None else: self.game_state.gamer.name = None return if not self.check(message): #проверка состояния self.start_game() continue if message.username == self.game_state.leader.name: self.game_state.leader.last_message = message.message else: self.game_state.gamer.last_message = message.message self.broadcast(message) if message.username == self.game_state.leader.name: for i in range(0, 7): if not self.stop: if i==0: time.sleep(0.1) else: time.sleep(0.9) self.counter += 1 self.server_message.message = str(7 - self.counter) self.broadcast(self.server_message) else: break if self.stop: continue else: self.stop = True if self.stop: self.finish(False) else: self.finish(True) self.start_game()
def exit(self): self.sock.close() for client in self.clients: client.close() print(CLOSING)
#!/usr/bin/env python import client if __name__=='__main__': client.connect() print client.close()
def close(): cl.close()
def p_close(p): 'expression : CLOSE' client.close()
#coding:utf-8 """ python % wait_time count """ import sys import client wait_time = int(sys.argv[1]) count = int(sys.argv[2]) host = '127.0.0.1' port = int(sys.argv[3]) timeout = wait_time + 50 client = client.TestClient(host, port, timeout) return_count = 0 for x in range(count): client.Wait(wait_time) return_count = return_count + 1 client.close() print return_count
def p_client_close(p): """close_client : CLOSE_CLIENT""" client.close(client_socket)