I'm currently building a http server with python/twisted.
This server must fetch content on another web server, store it locally and send the response back to the client. If it encounters a 404, it must try to serve the local file, or will fail if there's no local file at all.
To achieve this, I'm using differed requests. My code roughly looks like this:
class RemoteFile(Resource):
isLeaf = True
def render_GET(self, request):
# Start downloading the corresponding file on the remote server
# Will call `self.serve_local_file` when done
return NOT_DONE_YET
def serve_local_file(self, request):
if self.file_exists_for_request(request):
fileResource = File(filename)
fileResource.render(request)
else:
page = NoResource("No such file")
html = page.render(request)
# The next two lines are my problem
request.write(html)
request.finish()
I had to add the last two lines (write()
and finish()
) in order to get the request to actually finish. I don't need these lines for a File
resource, because it handles the write/finish by itself, unlike the NoResource.
This is not a problem per se, but it turns out that this page
variable is not hardcoded, and it's actually a fallback
attribute on my class, which is supposed to be a Resource
. It means that I could, in the future, want to replace it with a default File
. In which case I'll have to modify my code...
Am I doing something wrong, or is this an inconsistency in the interfaces of File
and ErrorPage
?