title: Chat / Server client Python
body: <p>I am currently doing a project for university, I have to create a multiple chat client and server in python 3.4. 
  For some reason it will connect to one client but when a second client tries to connect it does nothing. However when the first client disconnects the other client will connect to it. Does anyone have any ideas, I have been trying to work this out for over 3 hours. </p>
  
  <p><strong>The Client Server</strong></p>
  
  <pre><code>import socket
  
  def Main():
      print("Send 'q' to exit\n")
      address = str(input("ip:port -&gt; "))
      nick = input("nick: ")
  
      try:
          if address.index(":") != 0:
              host = address[:address.index(":")]
              port = int(address[address.index(":")+1:])
      except ValueError:
          host = address
          port = 5000
  
      s = socket.socket()
      s.connect((host, port))
  
      message = input("-&gt; ")
  
      while message != "q":
          send_message = message + "pPp" + nick
          send_message2 = send_message.encode("UTF-8")
          s.send(bytes(send_message2))
          data = s.recv(1024)
          data_decoded = data.decode("UTF-8")
          data2 = data_decoded
          print(data_decoded)
          messageServer = str(data_decoded[:data_decoded.index("pPp")])
          nickServer = str(data_decoded[data_decoded.index("pPp")+3:])
  
          if not data_decoded == data2:
              print(nickServer + ": " + messageServer)
          message = input("-&gt; ")
      s.close()
  
  if __name__ == "__main__":
      Main()
  </code></pre>
  
  <p><strong>The server side:</strong></p>
  
  <pre><code>import socket
  import time
  import os
  from threading import Thread
  folderPath = "Chat Logs"
  filePath = folderPath + "/" + str(time.strftime("%H-%M-%S_%d-%m-%Y")) + ".txt"
  def clientHandler(c):
      while True:
          data = c.recv(1024)
          if not data:
              break
          data_decoded = data.decode("UTF-8")
          message = str(data_decoded[:data_decoded.index("pPp")])
          nick = str(data_decoded[data_decoded.index("pPp")+3:])
          print(nick + "$" + message)
          saveChat(nick, message)
          print("   Sending: " + data_decoded)
          c.send(bytes(data_decoded.encode("UTF-8")))
      c.close()
  def saveChat(nick, message):
      if not os.path.exists(folderPath):
          os.makedirs(folderPath)
      if not os.path.exists(filePath):
          f = open(filePath, "a")
          f.close()
      f = open(filePath, "a")
      f.write(nick + ": " + message + "\n")
      f.close()
  def Main():
      host = str(socket.gethostbyname(socket.gethostname()))
      port = 5000
      print(host + ":" + str(port) + "\n")
      Clients = int(input("Clients: "))
      s = socket.socket()
      s.bind((host, port))
      s.listen(Clients)
      while True:
          c, addr = s.accept()
          print("Connection from: " + str(addr))
          Thread(target=clientHandler(c)).start()
      s.close()
  if __name__ == "__main__":
      Main()
  </code></pre>