BaseHTTPServer and keep-alive (evil hack problem)

David hughjonesd at yahoo.co.uk
Mon Jul 26 09:35:37 EDT 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 =
self.rfile.read(int(self.headers.getheader("content-length")))

                # handle the message. 
                pm.handle_message(body, self.client_address[0],
self.wfile)
                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/threading.py", line 442, in __bootstrap
    self.run()
  File "/usr/lib/python2.3/threading.py", line 422, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/dhughj/blogfish/src/PeerManager.py", line 267, in
_passive_send
    wfile.write(message)
  File "/usr/lib/python2.3/socket.py", line 254, in write
    self.flush()
  File "/usr/lib/python2.3/socket.py", line 241, in flush
    self._sock.sendall(buffer)
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.

David



More information about the Python-list mailing list