[Twisted-web] web2: handling data from an http PUT request?

Allan Bailey allan at nefud.org
Fri Aug 11 13:08:58 CDT 2006

Does anyone have an example of how to handle data from an http PUT method?

So far I'm only getting partial data from the below example.

Note: I'm fairly new to twisted and using deferred's.  So if I'm missing something
please let me know, or send pointers.


class HOME(resource.Resource):
    addSlash = True
    def allowedMethods(self):
        return ('GET', 'HEAD', 'OPTIONS', 'PUT')

    def locateChild(self, req, segments):
        return (self, server.StopTraversal)

    def render_PUT(self, req):
        content = "<html><body>\n"

        content += "<hr>\n"
        content += "method: %s<br>\n" % req.method
        content += "uri: %s<br>\n" % req.uri
        content += "path: %s<br>\n" % req.path
        content += "dir(req): %s<br>\n" % dir(req)
        content += "<hr>\n"
        content += "args: %s<br>\n" % req.args
        content += "<hr>data from PUT:\n\n<hr>\n\n%s\n\n<hr>\n"
        content +="</body></html>\n"

        print req.stream
        print dir(req.stream)
        data = req.stream.read()

        if type(data) == type(""):
	    # this never happens, seems to always be a deferred. dunno why.
            content = content % data
            return http.Response(responsecode.OK, stream=content)

        elif isinstance(data, defer.Deferred):
            def _pull(result, send=content):
	        # result  should be a string of data.
                send = send % result
                return http.Response(responsecode.OK, stream=send)

            d = data.addCallback(_pull, content)

            return d
            raise 'FixMe'
        #return None
    def http_PUT(self, req):
        return self.render_PUT(req)


More information about the Twisted-web mailing list