Esempio n. 1
0
    def sendfilehelper(oldHandle, f):
        #Keep the checksum for future reference
        sum = Checksum.md5sum(filename)

        for i in range(_tries):
            if(isinstance(f, str)):
                #Open the file
                f = file(f, "rb")
            else:
                #Reset the file-like object the beginning
                f.reset()

            #Get the file size and send
            self.write(str(os.path.getsize(filename)) + '\n')
            self.flush(noremove = True)

            yield None

            #Get the file data and send
            data = f.read(_chunkSize)
            while(data):
                self.write(data)
                while(self.wBuf):
                    self.flush(noremove = True)
                    yield None
                data = f.read(_chunkSize)
            f.close()

            ##Wait for checksum
            
            #Mark this socket not waiting to read, but have us wake on input
            delWriteChannel(self)
            temp = self.handleInput
            self.handleInput = self.handleOutput

            #Actually receive the checksum
            check = self.readline()
            while(not check):
                yield None
                check = self.readline()

            #Restore the socket's state
            self.handleInput = temp
            addWriteChannel(self)

            #Send response
            if(check[:-1] == sum):
                self.write('done\n')
                self.flush()
                break
            else:
                self.write('retr\n')
                self.flush(noremove = True)
                
        #Restore the original method
        sock.handleOutput = oldHandle
        yield 0
Esempio n. 2
0
    def recvfilehelper(oldHandle, f):
        for i in range(_tries):
            if(isinstance(f, str)):
                #Open the file
                f = file(f, "wb")
            else:
                #Reset the file-like object the beginning
                f.reset()

            #Recieve the file size
            size = self.readline()
            while(not size):
                yield None
                size = self.readline()
            size = int(size[:-1])

            yield None

            #Receive the data
            while 1:
                data = self.read(size)
                size -= len(data)
                f.write(data)
                if(size <= 0):
                    break
                yield None
            f.close()

            #Send checksum back
            self.write(Checksum.md5sum(filename) + '\n')
            self.flush()

            #Wait for response
            response = self.readline()
            while(not response):
                yield None
                response = self.readline()
            response = response[:-1]

            #See if we're finished
            if(response == 'done'):
                break

        #Restore the original method
        self.handleInput = oldHandle
        yield 0