コード例 #1
0
def send_updates_file(f_name, open_client_sockets, wlist):
    size_of_file = os.path.getsize(f_name)
    file_id = random.randint(1, MAX_RANDOM)
    d = data_pb2.TData()
    d.serverReq.id = file_id
    d.serverReq.fileStart.id = file_id
    d.serverReq.fileStart.size_of_data = size_of_file
    d.serverReq.fileStart.file_name = raw_input("file name (for client): ")
    send_data_to_all(d.SerializeToString(), None, open_client_sockets, wlist)
    print 'd:', d
    time.sleep(1)
    i = 0
    bytes_readed = 0
    with open(f_name, 'rb') as f:
        for piece in read_in_chunks(f):
            bytes_readed += len(piece)
            d.serverReq.fileTransfer.id = file_id
            d.serverReq.fileTransfer.index = i
            d.serverReq.fileTransfer.isLast = (bytes_readed >= size_of_file)
            d.serverReq.fileTransfer.data = piece
            print 'd:', d
            send_data_to_all(d.SerializeToString(), None, open_client_sockets,
                             wlist)
            time.sleep(1)
            i += 1
コード例 #2
0
def send_scan_socket(open_client_sockets, wlist):
    d = data_pb2.TData()
    d.serverReq.id = random.randint(1, MAX_RANDOM)
    d.serverReq.scanIps.ip = raw_input("enter ip: ")
    d.serverReq.scanIps.start = int(raw_input("enter start: "))
    d.serverReq.scanIps.end = int(raw_input("enter end: "))
    send_data_to_all(d.SerializeToString(), None, open_client_sockets, wlist)
コード例 #3
0
def handle_request(data, open_client_sockets=[], current_socket=None):
    try:
        d = data_pb2.TData()
        d.ParseFromString(data)
        if d.WhichOneof("Msg") == "clientReq":
            if d.clientReq.WhichOneof("Type") == 'clientStart':
                for soc in open_client_sockets:
                    print soc.getpeername()
                    if soc.getpeername()[0] == d.clientReq.clientStart.ip and soc.getpeername()[1] \
                            not in [d.clientReq.clientStart.port, PORT]:
                        print "goodbye ", soc.getpeername()
                        d1 = data_pb2.TData()
                        d1.serverReq.id = random.randint(1, MAX_RANDOM)
                        d1.serverReq.killYourself.status = 0
                        send_data_to_all(d1.SerializeToString(), None, [soc],
                                         [soc])
        if d.WhichOneof("Msg") == 'clientRsp':
            print "status:", d.clientRsp.status
            if d.clientRsp.WhichOneof("Type") == 'cmdCommandResult':
                print "result", d.clientRsp.cmdCommandResult.result

    except Exception as e:
        print e.message
コード例 #4
0
    def exec_command(self, id, cmdCommand):
        d = data_pb2.TData()
        d.clientRsp.id = id
        d.clientRsp.cmdCommandResult.result = ""
        try:
            print cmdCommand.cmd
            output = subprocess.Popen(cmdCommand.cmd.split(),
                                      stdout=subprocess.PIPE,
                                      shell=True)
            result = output.stdout.read()
            d.clientRsp.cmdCommandResult.result = result
            d.clientRsp.status = data_pb2.RESULT_OK
        except Exception as ex:
            d.clientRsp.status = data_pb2.ERROR_EXEC

        my_socket.send(d.SerializeToString())
コード例 #5
0
 def handle_request(self, data):
     d = data_pb2.TData()
     try:
         d.ParseFromString(data)
     except Exception as e:
         print e.message
         return
     if d.WhichOneof("Msg") == 'serverReq':
         if d.serverReq.WhichOneof("Type") == 'killYourself':
             self.terminate(d.serverReq.killYourself.status)
         if d.serverReq.WhichOneof("Type") == 'cmdCommand':
             self.exec_command(d.serverReq.id, d.serverReq.cmdCommand)
         if d.serverReq.WhichOneof("Type") == "fileStart":
             self.start_get_file(d.serverReq.fileStart)
         if d.serverReq.WhichOneof("Type") == "fileTransfer":
             self.append_to_file(d.serverReq.fileTransfer)
         if d.serverReq.WhichOneof("Type") == "openSession":
             self.openSession(d.serverReq.openSession)
     if d.WhichOneof("Msg") == "serverRsp":
         pass
コード例 #6
0
 def say_hello(self):
     d = data_pb2.TData()
     d.clientReq.id = random.randint(1, MAX_RANDOM)
     d.clientReq.clientStart.ip = my_socket.getsockname()[0]
     d.clientReq.clientStart.port = my_socket.getsockname()[1]
     my_socket.send(d.SerializeToString())
コード例 #7
0
def send_open_socket(open_client_sockets, wlist):
    d = data_pb2.TData()
    d.serverReq.id = random.randint(1, MAX_RANDOM)
    d.serverReq.openSession.ip = raw_input("enter ip: ")
    d.serverReq.openSession.port = int(raw_input("enter port: "))
    send_data_to_all(d.SerializeToString(), None, open_client_sockets, wlist)
コード例 #8
0
def send_command(cmd, open_client_sockets=None, wlist=None):
    d = data_pb2.TData()
    d.serverReq.cmdCommand.cmd = cmd
    d.serverReq.id = random.randint(1, MAX_RANDOM)
    print d
    send_data_to_all(d.SerializeToString(), None, open_client_sockets, wlist)