A sample python dependent web application increases the number of requests by 2, why?

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 #1 

Then refresh the page and see:

 I am request #2 

However, I see:

 I am request #3 

When I update again, I see:

 I am request #5 

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.

+6
source share
2 answers

Browsers can behave amazingly. If you try to print the complete request, you can find, for example, the request "/" as well as "favicon.ico".

+7
source

The browser can make a second request for favicon.ico .

You need your server to print the location of the request when it receives the request. This will tell you if this is correct.

+2
source

Source: https://habr.com/ru/post/908286/


All Articles