示例#1
0
    def send_file_udp(s, filename):
        bytes_sent = 0

        sending_file = open(filename, 'rb')
        filesize = os.stat(filename).st_size

        while True:
            package, client_address = s.recvfrom(Constants.FILE_CHUNK_SIZE)
            unpacked_package = Utils.unpack_package(package)

            if unpacked_package['command'] == Constants.INIT_TRANSMIT:
                bytes_sent = int(unpacked_package['payload'])
                sending_file.seek(bytes_sent)
                data = sending_file.read(Constants.FILE_CHUNK_SIZE)
                if not data:
                    s.sendto(Utils.pack_package(Constants.FIN, ''), client_address)
                    sending_file.close()
                    break
                else:
                    s.sendto(Utils.pack_package(Constants.ACK, data), client_address)

            package, client_address = s.recvfrom(Constants.FILE_CHUNK_SIZE)
            unpacked_package = Utils.unpack_package(package)

            if unpacked_package['command'] == Constants.ACK:
                bytes_sent += len(data)
                percent = int(float(bytes_sent) * 100 / float(filesize))

                print "{0} / {1} Kb sent ({2}%)".format(Utils.to_kilobytes(bytes_sent),
                                                        Utils.to_kilobytes(filesize), percent)
示例#2
0
    def send_file_multicast(s, filename):
        connections = {}
        filesize = os.stat(filename).st_size
        try:
            while True:
                readable, _, _ = select.select([s], [], [])
                for rd in readable:
                    bytes_sent = 0
                    package, client_address = s.recvfrom(Constants.FILE_CHUNK_SIZE)
                    unpacked_package = Utils.unpack_package(package)

                    if not connections.has_key(client_address) or connections[client_address] is None:
                        connections[client_address] = open(filename, 'rb')

                    if unpacked_package['command'] == Constants.INIT_TRANSMIT:
                        bytes_sent = int(unpacked_package['payload'])
                        connections[client_address].seek(bytes_sent)
                        data = connections[client_address].read(Constants.FILE_CHUNK_SIZE)
                        if not data:
                            rd.sendto(Utils.pack_package(Constants.FIN, ''), client_address)
                            connections[client_address].close()
                            connections[client_address] = None
                        else:
                            rd.sendto(Utils.pack_package(Constants.ACK, data), client_address)

                    bytes_sent += len(data)
                    percent = int(float(bytes_sent) * 100 / float(filesize))

                    print "{0} / {1} Kb sent to client {2}({3}%)".format(Utils.to_kilobytes(bytes_sent),
                                                                         Utils.to_kilobytes(filesize), client_address,
                                                                         percent)
                    sys.stdout.write('\033M')

        except socket.error, value:
            print value
示例#3
0
    def receive_file_multicast(s, filename, host, port):
        receiving_file = open(filename, 'ab')

        bytes_received = os.path.getsize(filename)
        s.settimeout(Constants.DEFAULT_TIMEOUT)
        while True:
            s.sendto(Utils.pack_package(Constants.INIT_TRANSMIT, str(bytes_received)), (host, port))
            response, server_address = s.recvfrom(Constants.FILE_CHUNK_SIZE)

            unpacked_response = Utils.unpack_package(response)
            if not unpacked_response['command'] == Constants.FIN:
                receiving_file.write(unpacked_response['payload'])
            else:
                print '\nFile received'
                receiving_file.close()
                break
            bytes_received += len(unpacked_response['payload'])

            print "Received {0} Kb from {1}".format(Utils.to_kilobytes(bytes_received), server_address)
            sys.stdout.write('\033M')
        s.close()