Hi Mark,

On Tue, 17 Apr 2007 15:49:10 -0500, Mark Feldman <[EMAIL PROTECTED]> wrote:

Does anyone have a simple example of processing either a post or put on the server side using web2? I've had no problem serving up web pages from a variety of sources (files, database backends). The changes needed to support data back fro the user -- undoubtedly a set of deferred methods in the right place -- escape me. Thanks.


For POST, you could use resource.PostableResource, which will take
care of parsing your run-of-the-mill form posting.  Just subclass
resource.PostableResource, and implement the "render" method.

For PUT, and also for cases where your are POSTING non-form data,
e.g., an XML payload, you need to subclass resource.Resource,
implement the desired method (http_POST, http_PUT), and then hook
the request.stream up to a processing chain.

Example:

from twisted.web2 import resource, stream


class Foo(resource.Resource):
    _allowedMethods = ('POST', 'PUT')

    def _finished(self, result, request):
        # return some http.Response

    def _failed(self, reason, request):
        # return some http.Response

    def http_POST(self, request):
        def handleData(data):
            # process each chunk of data as it arrives

        # readStream will keep calling handleData until the
        # entire stream has been read.
        d = stream.readStream(request.stream, handleData)

        # these callbacks/errbacks will be called when readStream
        # has finished reading the stream.
        d.addCallbacks(
            self._finished,
            self._failed,
            callbackArgs=(request,),
            errbackArgs=(request,)
        )

        # Catch any errors that occur in self._finished.
        d.addErrback(self._failed, request)

        return d

    def http_PUT(self, request):
        # Just handle things the same way POST does
        return self.http_POST(request)


Hope this helps,

L. Daniel Burr

_______________________________________________
Twisted-web mailing list
[email protected]
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web

Reply via email to