[Twisted-web] web2 and database handling

Mircea Amarascu mircea at ag-projects.com
Wed Dec 13 09:59:52 CST 2006


Hello,

I want to attach the result of a database SELECT query to the response of
a HTTP GET request.

In twisted.web I've done it this way:

------------------------------
from twisted.web import resource, server, http
from twisted.enterprise import adbapi

class Site(resource.Resource):
     ...
   
    def render(self, request):
        query = "SELECT data FROM test"
        self.db.runQuery(query).addCallback(
            self._gotData, request).addErrback(
            self._dbError, request)
        return server.NOT_DONE_YET

    def _gotData(self, results, request):
        request.write("%s" % data[0])
        request.write(body)
        request.finish()

    def _dbError(self, failure, request):
        request.setResponseCode(http.INTERNAL_SERVER_ERROR)
        request.write("Error fetching data.")
        request.finish()
------------------------------

What is the best way to perform the same operation in web2? A
web2.server.NOT_DONE_YET constant is not defined.

In other words, how could I return a defer-related response
instead of a simple http.Response object ?

Thank you.



More information about the Twisted-web mailing list