title: Creating a multithreaded server using SocketServer framework in python
body: <pre><code>#!/usr/bin/env python
  #-*- coding: utf-8 -*-
  
  import sys
  sys.dont_write_bytecode = True
  import shlex
  import subprocess
  import SocketServer
  
  sess = []
  
  class TCPHandler(SocketServer.BaseRequestHandler):
      def handle(self):
          global sess
          sess.append(self.request)
          ip,port = self.client_address
          print "#%d: client %s:%d"%(len(sess),ip,port)
          while True:
              cmd = self.request.recv(8192)
              out = subprocess.check_output(shlex.split(cmd),stderr=subprocess.STDOUT,shell=True)
              self.request.send(out)
          self.request.close()
  class ThreadedTCPServer(SocketServer.ThreadingMixIn,SocketServer.TCPServer): pass
  
  if __name__ == "__main__":
      port = 4242
      svr = ThreadedTCPServer(("",port),TCPHandler)
      print ":%d"%port
      svr.serve_forever()
  </code></pre>