Python CustomResponse

From Ladon Webservice

Jump to: navigation, search

Custom Responses

In some cases it can be nessecary to override the normal response system and return something completely different - ie.

  1. if a certain method should return a file as a http attachment response for browsers (Content-Disposition: attachment;).
  2. or a commandline tool that sends a SOAP request and should output raw text as result

For this purpose a class named CustomResponse has been defined in ladon.server.customresponse.

Objects of CustomResponse descendents are intercepted in Ladons's WSGI application part so the service developer has full control over response headers and data.


To make a custom response class 2 methods must be implemented. response_headers() returns the headers to be returned in the response and response_data() must return the response data.

Here is an example that returns an attachment as a browser download response:

from ladon.server.customresponse import CustomResponse
class HTTPAttachmentResponse(CustomResponse):
        def __init__(self,fileobj,filename,filesize,blocksize=4096):
                self.fileobj = fileobj
                self.filename = filename
                self.filesize = filesize
                self.blocksize = blocksize
        def response_headers(self):
                # TODO: Handle encodings for filenames
                return [
                        ('Content-Disposition','attachment; filename="%s"' % self.filename.encode('utf-8')),
        def response_data(self):
                return iter(lambda:, '')