stopping SimpleHTTPServer

Alex Martelli aleax at aleax.it
Wed Nov 20 06:55:19 EST 2002


dsavitsk wrote:

> "Alex Martelli" <aleax at aleax.it> wrote in message
> news:AptC9.28393$744.1027998 at news1.tin.it...
>> dsavitsk wrote:
> 
> * * *
> 
>> In your subclass, you need to override method handle_error, which by
>> default processes all exceptions (including the SystemExit exception
>> raised by sys.exit) by printing a traceback and continuing.  Check if
>> the exception is SystemExit and then re-raise it, otherwise delegate
>> to your base-class's handle_error for normal error processing...
>>
> right.  I am not getting something very fundamental, so this is a good
> learning opportunity ...

Here's the simplest toy example I can put together -- it may help e.g. in
case you made any confusion between handler and server classes:

import BaseHTTPServer as B
import SimpleHTTPServer as S
import sys

class myServer(B.HTTPServer):
    def handle_error(self, request, client_address):
        print 'handle_error', request, client_address
        raise

class myHandler(S.SimpleHTTPRequestHandler):
    def do_GET(self):
        sys.exit(1)

S.test(myHandler, myServer)

running this, then doing e.g. 
urllib.urlopen('http://localhost:8000/whatever')
from another terminal on an interactive session, results in output:

Serving HTTP on 0.0.0.0 port 8000 ...
handle_error <socket object, fd=4, family=2, type=1, protocol=0> 
('127.0.0.1', 50636)

and a return to the prompt.


Alex




More information about the Python-list mailing list