Python hash function as Twisted xmlrpc class issueing same has for every file?
Question: I'm new to most of this so forgive me if I'm doing something really dumb. The
following is a simple Twisted xmlrpc server which is supposed to return file
info. It works fine except that the `xmlrpc_hash` function gives the same
result for every file. Example below code. Any help would be great!

    
    
    from twisted.web import xmlrpc, server
    import os
    
    class rfi(xmlrpc.XMLRPC):
        """
        rfi - Remote File Info server
        """
    
        def xmlrpc_echo(self, x):
            """
            Return all passed args as a test
            """
            return x
    
        def xmlrpc_location(self):
            """
            Return current directory name
            """
            return os.getcwd()
    
        def xmlrpc_ls(self, path):
            """
            Run ls on the path
            """
            result = []
            listing = os.listdir(path)
            for l in listing:
                result.append(l)
            return result
    
        def xmlrpc_stat(self, path):
            """
            Stat the path
            """
            result = str(os.stat(path))
            return result
    
        def xmlrpc_hash(self, path):
            """
            Hash the path
            """
            from hashlib import sha1
            if os.path.isfile(path):
                f = open(path,'rb')
                h = sha1()
                block_size = 2**20
                f.close()
                return h.hexdigest()
            else:
                return 'Not a file'
    
    if __name__ == '__main__':
        from twisted.internet import reactor
        r = rfi()
        reactor.listenTCP(7081, server.Site(r))
        reactor.run()
    

Example output:

    
    
    import xmlrpclib
    s = xmlrpclib.Server('http://localhost:7081/')
    s.hash('file_1.txt')
    'da39a3ee5e6b4b0d3255bfef95601890afd80709'
    s.hash('file_2.txt')
    'da39a3ee5e6b4b0d3255bfef95601890afd80709'
    


Answer: This is because you're never actually updating the hash object:

    
    
        from hashlib import sha1
        if os.path.isfile(path):
            f = open(path,'rb')
            h = sha1()
            h.update(f.read()) # You're missing this line
            f.close()
            return h.hexdigest()
        else:
            return 'Not a file'
    

