BaseHTTPServer and keep-alive (evil hack problem)

David hughjonesd at
Mon Jul 26 15:35:37 CEST 2004

This is called "evil hack problem" because my problems are due to me
trying to do an evil hack. Please don't judge me, just believe that
there are reasons for what I'm trying to do :-)

I've got a problem with BaseHTTPServer. I am trying to keep a
connection alive so that I can send data down it. At the moment, I
have subclassed BaseHTTPRequestHandler like this:

        def do_POST(self):
                body ="content-length")))

                # handle the message. 
                pm.handle_message(body, self.client_address[0],
                debug("sending keep-alive header")
                self.send_header("Connection", "keep-alive")

The handle_message method puts the wfile into a global map. When the
server wants to send more data, it just writes to the wfile. This
occurs in a separate thread.
So the do_POST method swiftly returns. I send the "keep-alive" header
because it seems to set connection_closed to 0 in the
BaseHTTPRequestHandler code, rather than because the client needs to
hear it.

However, when I try and write data to the wfile, I get:

Exception in thread Thread-3:Traceback (most recent call last):
  File "/usr/lib/python2.3/", line 442, in __bootstrap
  File "/usr/lib/python2.3/", line 422, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/dhughj/blogfish/src/", line 267, in
  File "/usr/lib/python2.3/", line 254, in write
  File "/usr/lib/python2.3/", line 241, in flush
AttributeError: 'NoneType' object has no attribute 'sendall'

Can anyone help me to keep the connection alive at both ends, or
explain why at the moment the server is closing the conenection?

Thanks a lot.


More information about the Python-list mailing list