Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing static.File

I'm new to Twisted and I'm having trouble with some necessary subclassing for the static.File in twisted. i'm trying to set request headers within the subclass.

class ResponseFile(static.File):

    def render_GET(self, request):
        request.setHeader('Content-Disposition', ['attachment ; filename="tick_db_export.csv"'])
        static.File.render_GET(self, request)

if __name__ == "__main__":
    from twisted.internet import reactor
    root = ResponseFile('WebFolder')
    testHandler = TestHandler()
    root.putChild('main', testHandler)
    reactor.listenTCP(3650, server.Site(root))
    reactor.run()

The first bit of code is the subclass definition itself (pretty straightforward), while the second bit is the initialization portion from my code (this isn't all of my code). I've have also subclassed a resource.Resource object called TestHandler. WebFolder is another folder containing many static files.

However, I am getting most of these types of exception when making calls to the server.

Unhandled Error
Traceback (most recent call last):
Failure: exceptions.RuntimeError: Producer was not unregistered for /

With many different paths other than root.

like image 782
jab Avatar asked Oct 07 '22 22:10

jab


1 Answers

The problem in your code is in render_GET method. It returns nothing. Basically it must return string for synchronous response and NOT_DONE_YET value for asynchronous response. In your case render_GET returns None (and your connection get closed immediately).

So you have to make a smaller change in your render_GET (add proper return):

def render_GET(self, request):
    request.setHeader('Content-Disposition', ['attachment ; filename="tick_db_export.csv"'])
    return static.File.render_GET(self, request)

If you inspect twisted.web.static.py module you'll find that File.render_GET makes producer and returns NOT_DONE_YET which makes connection to hold on until it is not explicitly closed (in our case, after file is downloaded).

like image 110
Rostyslav Dzinko Avatar answered Oct 10 '22 03:10

Rostyslav Dzinko