[Twisted-Python] using defers with requests

charles brandt twisted at st.imul.us
Sat Nov 15 20:59:05 MST 2003


> 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 [-] <GET /beta/resource3.rpy HTTP/1.1>






More information about the Twisted-Python mailing list