[Twisted-web] web2 and database handling
David Reid
dreid at dreid.org
Wed Dec 13 11:18:59 CST 2006
On Dec 13, 2006, at 7:59 AM, Mircea Amarascu wrote:
> 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.
You can return a defer.Deferred() that'll be fired with an
http.Response. Alternatively if you have a lot of data, it may be
useful to return an http.Response immediately, and use a stream
object to write the data incrementally. (see twisted.web2.stream for
information on this) Please note however that the stream API is not
finalized, and in fact a new alternative implementation is just
awaiting it's author having time to finish it.
-David
> _______________________________________________
> Twisted-web mailing list
> Twisted-web at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
David Reid
http://dreid.org/
More information about the Twisted-web
mailing list