How to check in CGI if client disconnected

Vishal vrshah at
Mon Aug 25 08:26:19 CEST 2008


  Thanks for the replies. In my case, the cgi is sending a large file
to the client. In case the the stop button is pressed on the browser
to cancel the download, i want to do some cleanup action. It's all one-
way transfer in this case, so i can't expect the client to send
anything to me. I read somewhere that apache sends the SIGTERM signal
to a cgi when the client disconnects. However, my cgi is not getting
the signal - is there a way to have the cgi catch and handle the

I tried using the signal module

def sigtermHandler(signum, frame):
    # do some cleanup

signal.signal(signal.SIGTERM, sigtermHandler)


But even this doesn't work.


On Aug 25, 2:58 am, "Gabriel Genellina" <gagsl-... at>
> En Sun, 24 Aug 2008 17:51:36 -0300, Wojtek Walczak <gmin... at bzt.bzt> escribió:
> > On Sun, 24 Aug 2008 17:21:52 -0300, Gabriel Genellina wrote:
> >>>    I am writing a CGI to serve files to the caller. I was wondering if
> >>> there is any way to tell in my CGI if the client browser is still
> >>> connected. If it is not, i want to execute some special code before
> >>> exiting.
> >>>    Is there any way to do this? Any help on this is appreciated :)
> >> I don't think so. A CGI script runs once per request, and exits. The server may find that client disconnected, but that may happen after the script finished.
> > I am not a web developer, but I think that the only way is to
> > set a timeout on server side. You can't be sure that the client
> > disconnected, but you can stop CGI script if there's no
> > action on client side for too long.
> Which kind of client action? Every link clicked or form submitted generates a different request that triggers a CGI script; the script starts, reads its parameters, do its task, and exits. There is no "long running process" in CGI - the whole "world" must be recreated on each request (a total waste of resources, sure).
> If processing takes so much time, it's better to assign it a "ticket" - the user may come back later and see if its "ticket" has been finished, or the system may send an email telling him.
> --
> Gabriel Genellina

More information about the Python-list mailing list