Detect prematurely closed connection in web.py
Is there a way in Web.py to detect and handle connection being closed by user while the request is processing?
I tried setting unloadhook handler, but it doesn't get called in this case. It's only called after the request was successfully completed:
class handler:
def __init__(self):
pass
def GET(self):
try:
while(True):
pass
except Exception, e:
logger.debug(e)
def unload():
logger.debug('unloaded')
try:
app = web.application(urls, globals())
app.add_processor(web.unloadhook(unload))
application = app.wsgifunc()
except E开发者_StackOverflowxception, e:
logger.debug(e)
I open the app in a browser and when it starts spinning in the while loop I break the request, but no exception is thrown.
Due to the way that web servers and WSGI itself works, generally there is no reliable way of doing it. For an explanation read:
http://groups.google.com/group/modwsgi/browse_frm/thread/8ebd9aca9d317ac9
Set a timeout, and catch an exception type that specifically covers the case of a closed connection.
See this on exception handling in Python
精彩评论