So, SimpleHTTPRequestHandler inherits from BaseHTTPServer.BaseHTTPRequestHandler , which in turn inherits from SocketServer.StreamRequestHandler .
In SocketServer.StreamRequestHandler pseudo rfile and wfile are created in the setup() method from a socket object (known as self.connection ):
def setup(self): self.connection = self.request if self.timeout is not None: self.connection.settimeout(self.timeout) if self.disable_nagle_algorithm: self.connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True) self.rfile = self.connection.makefile('rb', self.rbufsize) self.wfile = self.connection.makefile('wb', self.wbufsize)
The self.connection socket is still available to you, so you can call self.connection.close() to close it. However, the pseudo wfile may contain buffered data that may be lost, so you could / should instead call self.finish() , also defined in SocketServer.StreamRequestHandler :
def finish(self): if not self.wfile.closed: self.wfile.flush() self.wfile.close() self.rfile.close()
So, I think the following should work (untested):
class StreamerHandler(SimpleHTTPRequestHandler): def do_POST(self): try: length = int(self.headers.getheader('content-length')) data = self.rfile.read(length) self.send_response(200, "OK") self.finish() process_data(data, self.client_address) except Exception as exc: logging.error( "{0}/{1}({2})".format( type(self).__name__, type(exc).__name__, str(exc)))
source share