title: Python socket programming with threads
body: <p>I am new to socket programming with threads in python. I have a simple server app:</p>
  
  <pre><code>#!/usr/bin/python
  from socket import *
  from threading import Thread
  import _thread
  
  host = ''  
  port = 52000 
  
  sock = socket()
  sock.bind((host, port))
  sock.listen(5) 
  print "server is waiting for connections.."
  def clientthread():
      conn, addr = sock.accept()
      print addr, "is connected"
      conn.send('Hi! I am server\n') #send only takes string
      #Receiving from client
      data = conn.recv(1024) 
      print data
  
  for num in range(5):
      Thread(target = clientthread).start() 
  
  sock.close()
  </code></pre>
  
  <p>This simple code works perfectly for 5 clients and after that when one logs out and a new one wishes to contact with server, the application simply closes. But what I want is to have 5 simultaneous connections, and once anyone have logged out, one client space is recreated to accommodate the new one (i.e. 5th one currently). I believe threading module can help me here. Its functions like <code>acquire()</code> and <code>release()</code>. But not sure how? Please help.</p>