Killing a socket server in DOS?

Steve Holden sholden at holdenweb.com
Fri May 31 09:14:04 EDT 2002


"Magnus Lie Hetland" <mlh at vier.idi.ntnu.no> wrote ...
> In article <MCHJ8.76519$%u2.4430 at atlpnn01.usenetserver.com>, Steve Holden
wrote:
> >"Magnus Lie Hetland" <mlh at vier.idi.ntnu.no> wrote ...
> [snip]
> >
> >Here's a snippet using asyncore that seems to do what you want. Are you
> >saying that you can't trap KeyboiardInterrupt? You'll have your own
> >http_server() equivalent already, I'm guessing.
> >
> [snip]
> >    try:
> >        asyncore.loop(1.0) # Frequent checks for interrupt
> >    except KeyboardInterrupt:
> >        print "Completed" # on console
> >        logfile.close()
>
> I guess the use of a timeout was the problem... I simply used
> asyncore.loop(). It worked in UNIX, but not in DOS (or Cygwin). So...
> The low timout means that the Python loop surrounding select() will be
> executed more often, thereby making it possible to interrupt it more
> often?
>
Yeah, there are enough differences in socket implementation that there's a
little roughness around the edges in porting network code between platforms.
Having said that, and having used both Windows (95, 98, NT, 2K) and Unix
(Linux, Solaris) I am amazed how easy it *is* to write portable code. The
timeout defaults to 30 seconds, and very few people have the patience to
wait that long after ^C. On Unix I'm *guessing* that the socket-related
system calls are interruptible. On Windows I have hard evidence (in the
shape of hung programs) that they aren't.

> Now, it seems I have the same problem with another server too, but
> that one is written using SocketServer (SimpleXMLRPCServer, actually).
> Not sure how to achieve the same there. It seems I'm not able to
> interrupt its serve_forever() in DOS/Cygwin there either -- although I
> can easily interrupt it in UNIX (as with asyncore).
>
I don't know SinpleXMLRPCServer, but I've written about SocketServer (which
meant I had to understamd it :-)

> Any similar tricks I can use there, to force it to listen for
> interrupts?
>

For some reason the SocketServer library module (and therefore its
dependants) catches *all* errors in the handle_request() (?) method:

        if self.verify_request(request, client_address):
            try:
                self.process_request(request, client_address)
            except:
                self.handle_error(request, client_address)
                self.close_request(request)

You will therefore have to either subclass it and override handle_request(),
or rewrite the library. The handle_request() method isn't that long.
Personally I have in tha past simply added a conditional to re-raise the
exception if and only if it's a KeyboardInterrupt, and that worked.

Note that SocketServer and buddies are intended more as demonstration code
than as production-quality stuff. Asyncore is much more robust, though even
that needed tweaks for Zope.

regards
--
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list