The sample code for the underlying web server provided by http://twistedmatrix.com/trac/ seems to increase the request counter by two for each request, rather than 1.
The code:
from twisted.web import server, resource from twisted.internet import reactor class HelloResource(resource.Resource): isLeaf = True numberRequests = 0 def render_GET(self, request): self.numberRequests += 1 request.setHeader("content-type", "text/plain") return "I am request #" + str(self.numberRequests) + "\n" reactor.listenTCP(8080, server.Site(HelloResource())) reactor.run()
After looking at the code, it looks like you should connect to the URL http: // localhost: 8080 and see:
I am request
Then refresh the page and see:
I am request
However, I see:
I am request
When I update again, I see:
I am request
So, judging by the counter, the server seems to call the render_GET function twice for each request. I am running this on Windows 7 using Python 2.7. Any idea what might happen or is this the expected behavior?
Update: The code works fine, it is a browser that is complicated. Refreshing each page, the browser sends a GET request for "/" and "/favicon.ico", which takes into account an increase of 2, because the render_GET function is actually called twice to refresh the page.
source share