[code.view]

[top] / python / PyMOTW / urllib2 / urllib2_nfs_handler.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2009 Doug Hellmann All rights reserved.
     #
     """
     """
     #end_pymotw_header
     
     import mimetypes
     import os
     import tempfile
     import urllib
     import urllib2
     
     class NFSFile(file):
         def __init__(self, tempdir, filename):
             self.tempdir = tempdir
             file.__init__(self, filename, 'rb')
         def close(self):
             print
             print 'NFSFile:'
             print '  unmounting %s' % self.tempdir
             print '  when %s is closed' % os.path.basename(self.name)
             return file.close(self)
     
     class FauxNFSHandler(urllib2.BaseHandler):
         
         def __init__(self, tempdir):
             self.tempdir = tempdir
         
         def nfs_open(self, req):
             url = req.get_selector()
             directory_name, file_name = os.path.split(url)
             server_name = req.get_host()
             print
             print 'FauxNFSHandler simulating mount:'
             print '  Remote path: %s' % directory_name
             print '  Server     : %s' % server_name
             print '  Local path : %s' % tempdir
             print '  File name  : %s' % file_name
             local_file = os.path.join(tempdir, file_name)
             fp = NFSFile(tempdir, local_file)
             content_type = mimetypes.guess_type(file_name)[0] or 'application/octet-stream'
             stats = os.stat(local_file)
             size = stats.st_size
             headers = { 'Content-type': content_type,
                         'Content-length': size,
                       }
             return urllib.addinfourl(fp, headers, req.get_full_url())
     
     if __name__ == '__main__':
         tempdir = tempfile.mkdtemp()
         try:
             # Populate the temporary file for the simulation
             with open(os.path.join(tempdir, 'file.txt'), 'wt') as f:
                 f.write('Contents of file.txt')
             
             # Construct an opener with our NFS handler
             # and register it as the default opener.
             opener = urllib2.build_opener(FauxNFSHandler(tempdir))
             urllib2.install_opener(opener)
     
             # Open the file through a URL.
             response = urllib2.urlopen('nfs://remote_server/path/to/the/file.txt')
             print
             print 'READ CONTENTS:', response.read()
             print 'URL          :', response.geturl()
             print 'HEADERS:'
             for name, value in sorted(response.info().items()):
                 print '  %-15s = %s' % (name, value)
             response.close()
         finally:
             os.remove(os.path.join(tempdir, 'file.txt'))
             os.removedirs(tempdir)
     

[top] / python / PyMOTW / urllib2 / urllib2_nfs_handler.py

contact | logmethods.com