-
Notifications
You must be signed in to change notification settings - Fork 5
Pylib httpfileserver
A server based on the BaseHTTPServer package that comes with Python. It uses the httpserver.File class to describe the files to host. It comes with a supercache option to tell the browser to cache aggressively, allowing much faster load times in the finished product, but the option should be totally off-limits during development, as caching will simply prevent the browser and the server from reading your changes.
Methods:
* __init__(address=('',80),files=[],supercache=False) # Host all files in the list at the given address.
* host(fileobj) # Adds the file's virtual paths to the server's list.
* start() # Starts hosting.
* close() # Stops hosting.
Hosting information relating a file path to a URL on the local server, as well as server caching configuration and mimetype.
Methods:
* __init__(root,end,spath=None,mimetype="text/plain", ...cache stuff...) # path = root+end, spath can be a string or a list of strings where to host it on the server (always starts with "/").
* load() # load file into memory if necessary.
Properties:
* value # The contents of the file. Will call self.load().
* updated # Whether the disk copy is more up-to-date than the RAM copy.
* fmtime # File modification time in epoch time.
Subclass of BaseHTTPRequestHandler. The server works in an odd but effective way, in which at the creation time of the server, the make_handler function is called to create a custom subclass of BaseFileHandler which can read that server's file hosting registration data.
Methods:
* do_GET() # Processes a GET request, calling self.files().
* files() # Defined by make_handler during the automatic subclassing process.
The thing about storing the hosting data as a dict, is that you can't pass it to the handler, you can only pass a copy as the dict existed at the time of passing. That's frustrating, since we want to be able to edit that dict after server initialization and have the handler reflect those changes. Class instances don't have this problem, since instead of copying, they're passed by reference. So the simple, easy way to pass a dict around by reference too? make a class called dict_by_reference, with a spec like this:
Methods:
* __init__(dict) # sets self.dict to dict
Properties:
* dict # internal dict within reference-passed instance
Returns an epoch datetime far in the future, for caching purposes.
Makes an anonymous subclass of BaseFileHandler with the function files() defined as "return dbr". Pass in a dict_by_reference, and it the handler will forever after be able to access its contents as self.files().dict.
A test function that, at the time of this writing, is severely deprecated and odd. Trying to run this module as a script will probably - no, I personally guarantee - running this unmodified will spit up errors and fail. A better test function should almost undoubtedly be written, but nobody cares about this particular speck of code so it's more likely to disappear some indeterminate time in the future than be rewritten right.