[Twisted-Python] using defers with requests
I am trying to select some data from a database in hopes of eventually using it to populate a dynamic page. Before I dive into woven, I was trying to get the following to print selected data directly to a html resource using the following code snippet (or various versions of it). I've only been able to get an empty page though ( <html><body></body></html> ). I'm wondering if maybe the request is getting returned before the callback makes it to the defer? Any ideas? Thanks in advance, -Charles. from twisted.web import resource, server from twisted.enterprise import adbapi dbpool = adbapi.ConnectionPool(<connection stuff>) def getData(data): return dbpool.runQuery("SELECT * FROM some_table WHERE some_column = %s", data) def printResult(data, request): request.write("pre-data") request.write(data) request.write("post-data") request.finish() class myResource(resource.Resource): def render(self, request): d = getData("some string") d.addCallback(printResult, request) return server.NOT_DONE_YET resource = myResource()
On Sat, Nov 15, 2003 at 06:57:38PM -0800, charles brandt wrote:
I am trying to select some data from a database in hopes of eventually using it to populate a dynamic page. Before I dive into woven, I was trying to get the following to print selected data directly to a html resource using the following code snippet (or various versions of it). I've only been able to get an empty page though ( <html><body></body></html> ). I'm wondering if maybe the request is getting returned before the callback makes it to the defer? Any ideas? Thanks in advance,
Are you sure your code is even being triggered? Since your code isn't writing out those HTML tags anywhere, it seems like some completely different resource is being renderred instead of yours. Can you put some debug prints in "render" and "printResult"? -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com/
On Sat, Nov 15, 2003 at 10:08:14PM -0500, Christopher Armstrong wrote:
On Sat, Nov 15, 2003 at 06:57:38PM -0800, charles brandt wrote:
I am trying to select some data from a database in hopes of eventually using it to populate a dynamic page. Before I dive into woven, I was trying to get the following to print selected data directly to a html resource using the following code snippet (or various versions of it). I've only been able to get an empty page though ( <html><body></body></html> ). I'm wondering if maybe the request is getting returned before the callback makes it to the defer? Any ideas? Thanks in advance,
Are you sure your code is even being triggered? Since your code isn't writing out those HTML tags anywhere, it seems like some completely different resource is being renderred instead of yours. Can you put some debug prints in "render" and "printResult"?
Also, Mozilla and IE I think both will say "<html><body></body></html>" even when nothing was sent. Try testing with telnet or wget... -Andrew.
On Sat, Nov 15, 2003 at 10:08:14PM -0500, Christopher Armstrong wrote:
On Sat, Nov 15, 2003 at 06:57:38PM -0800, charles brandt wrote:
I am trying to select some data from a database in hopes of eventually using it to populate a dynamic page. Before I dive into woven, I was trying to get the following to print selected data directly to a html resource using the following code snippet (or various versions of it). I've only been able to get an empty page though ( <html><body></body></html> ). I'm wondering if maybe the request is getting returned before the callback makes it to the defer? Any ideas? Thanks in advance,
Are you sure your code is even being triggered? Since your code isn't writing out those HTML tags anywhere, it seems like some completely different resource is being renderred instead of yours. Can you put some debug prints in "render" and "printResult"?
Also, Mozilla and IE I think both will say "<html><body></body></html>" even when nothing was sent. Try testing with telnet or wget...
Yes, I think Mozilla is the source for the "<html><body></body></html>"
string. I don't think anything is being sent. If I put a request.finish()
in "render" then the resource returns any data written to request in
"render" (but not "printResult"). I'm guessing that the connection with the
client gets closed before a request is rendered.
Is anyone aware of any examples of web applications written in twisted that
uses a database to store dynamic elements? Thats ultimately where I'm
trying to get with this. Thanks for the responses.
-Charles.
I added some print debug statements as follows:
def printResult(data, request):
print "entered printResult"
request.write("pre-data")
request.write(data)
request.write("post-data")
request.finish()
print request
return server.NOT_DONE_YET
class myResource(resource.Resource):
def render(self, request):
print "entered render"
d = getData("some string")
d.addCallback(printResult, request)
print "after callback"
return server.NOT_DONE_YET
resource = myResource()
which results in the following logs being generated:
2003/11/15 19:49 PST [HTTPChannel,13,<ip>] entered render
2003/11/15 19:49 PST [HTTPChannel,13,<ip>] after callback
2003/11/15 19:49 PST [HTTPChannel,13,<ip>] adbapi connecting: MySQLdb
{<connection args>}
2003/11/15 19:49 PST [-] entered printResult
2003/11/15 19:49 PST [-] <ip> - - [16/Nov/2003:03:49:11 +0000] "GET
/beta/resource3.rpy HTTP/1.1" 200 18 "-" "Mozilla/5.0"
2003/11/15 19:49 PST [-]
On Sat, Nov 15, 2003 at 07:59:05PM -0800, charles brandt wrote:
I added some print debug statements as follows:
def printResult(data, request): print "entered printResult" request.write("pre-data") request.write(data) request.write("post-data") request.finish() print request
What is 'data' here? can you print repr(data)? It might be the empty string, or something.
return server.NOT_DONE_YET
This is unnecessary here, FYI.
class myResource(resource.Resource): def render(self, request): print "entered render" d = getData("some string") d.addCallback(printResult, request) print "after callback" return server.NOT_DONE_YET
resource = myResource()
-- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com/
On Sat, Nov 15, 2003 at 07:59:05PM -0800, charles brandt wrote:
I added some print debug statements as follows:
def printResult(data, request): print "entered printResult" request.write("pre-data") request.write(data)
print repr(data)
request.write("post-data") request.finish() print request
What is 'data' here? can you print repr(data)? It might be the empty string, or something.
2003/11/15 20:43 PST [-] entered printResult 2003/11/15 20:43 PST [-] ((13L, 'Hawaii', 'HI', 'US'),) Seems like the request might be different from the original, or doesn't have a channel to respond on? -Charles
For completeness, Christopher pointed out that I was not passing a string to request.write (str(data)). Since I did not have an Errback call, the exception that was raised in the callback was never output. (fixed by adding: d.addErrback(log.err) ) Thanks for the help! -Charles
I added some print debug statements as follows:
def printResult(data, request): print "entered printResult" request.write("pre-data") request.write(data)
print repr(data)
request.write("post-data") request.finish() print request
What is 'data' here? can you print repr(data)? It might be the empty string, or something.
2003/11/15 20:43 PST [-] entered printResult 2003/11/15 20:43 PST [-] ((13L, 'Hawaii', 'HI', 'US'),)
Seems like the request might be different from the original, or doesn't have a channel to respond on?
-Charles
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (3)
-
Andrew Bennetts
-
charles brandt
-
Christopher Armstrong