Question

I am not new to Django (1y work experience) but I have got a strange task that I cannot deal with. So I have to write to write Middleware that will be counting requests, but only those that were not aborted by client (the ones that got fully rendered in browser).

I found out that when client aborts connection, server (Python) throws errno 32 Broken Pipe as it comes from unsuccessful write to socket [I think that connection server-client is TCP there so it is nothing strange].

But the problem is that I cannot find a way to check if browser fully rendered page or to check if that error occurred (of course that error can have another origin but it would be a good start).

Ohhh and I don't want to use JS (with JS it is a piece of cake) but I can use HTML5 (if it can help...)

Was it helpful?

Solution

I may have found the answer to your question, that is execute some code when the HTTPResponse has been sent. You can then increment what you want after the page has been fully sent to the client.

The example subclasses HttpResponse and returns an instance of the derived class explicitly in a view. If you have many views you don't want to override, maybe you could try this piece of code under your subclass definition

HttpResponse.__bases__ += (FullLoggingHttpResponse,)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top