Twisted: How to properly check that a request is finished <p>I'm creating an extremely simple Python script to serve one file to one person. However, I cannot seem to figure out how to fire a callback when the request is finished or broken. I was looking into <a href="https://twistedmatrix.com/documents/13.0.0/web/howto/web-in-60/interrupted.html" rel="nofollow">using deferreds</a> but I'm having a hard time wrapping my head around how they work.</p> <p>As far as I can tell my server runs everything synchronously, so I would expect this to be enough:</p> <pre><code>class FileResource(resource.Resource): isLeaf = True def __init__(self, filepath): self.filepath = filepath self.filename = os.path.basename(filepath) def render_GET(self, request): print '%s: request opened (%s)' % (get_time(), request.getClientIP()) request.setHeader('content-type', 'application/octet-stream') request.setHeader('content-disposition', 'attachment; filename="%s"' % self.filename) request.setHeader('content-length', os.path.getsize(self.filepath)) with open(self.filepath, 'rb') as f: request.write(f.read()) request.finish() print '%s: request closed (%s)' % (get_time(), request.getClientIP()) return server.NOT_DONE_YET </code></pre> <p>But the second print fires immediately. If anyone could point me in the right direct that would be appreciated.</p>