title: Python string being sent twice through sockets
body: <p>I'm improving this GUI-based chatroom script I found online. Right now I've been working on a way to add chat name tags, starting with a server host tag. This is a snip of my code that adds <code>[Server Host] </code> to someone's nickname if a connection with the host's IP is made:</p>
  <pre><code>...
  for key, value in addresses.items(): # Go through the currently connected addresses...
          if HOST in value: # ...and check if the host's IP is connected.
              name = &quot;[Server Host] &quot; + name # Add chat tag to the beginning of their nickname.
      msg = &quot;%s has joined the chat!&quot; % name
      broadcast(bytes(msg, &quot;utf8&quot;))
      clients[client] = name
  ...
  </code></pre>
  <p>However, when one joins the chat with the tag, it says <code>[Server Host] [Server Host] somename</code> for example instead of only one tag. Yet when chatting, the tag is just fine.</p>
  <p>Why does this happen?</p>
  <p>Bonus questions:</p>
  <ul>
  <li>Is it possible to add color to text in the chat, for tags and text for example?</li>
  <li>Every time a client attempts to exit by pressing the exit button on the window, the server's output has a large error message. How do I fix this so it's not annoying?</li>
  </ul>
  <p>Here's the code for the server:</p>
  <pre><code>from socket import AF_INET, socket, SOCK_STREAM, gethostbyname, gethostname
  from threading import Thread
  
  
  def accept_incoming_connections():
      &quot;&quot;&quot;Sets up handling for incoming clients.&quot;&quot;&quot;
      while True:
          client, client_address = SERVER.accept()
          print(&quot;%s:%s has connected.&quot; % client_address)
          client.send(bytes(&quot;Welcome! Type your name and press enter.&quot;, &quot;utf8&quot;))
          addresses[client] = client_address
          Thread(target=handle_client, args=(client,)).start()
  
  
  def handle_client(client):  # Takes client socket as argument.
      &quot;&quot;&quot;Handles a single client connection.&quot;&quot;&quot;
  
      name = str(client.recv(BUFSIZ).decode(&quot;utf8&quot;))
      welcome = 'Welcome, %s! To quit, type !quit.' % name
      client.send(bytes(welcome, &quot;utf8&quot;))
      for key, value in addresses.items(): # Go through the currently connected addresses...
          if HOST in value: # ...and check if the host's IP is connected.
              name = &quot;[Server Host] &quot; + name
      msg = &quot;%s has joined the chat!&quot; % name
      broadcast(bytes(msg, &quot;utf8&quot;))
      clients[client] = name
      while True:
              msg = client.recv(BUFSIZ)
              if msg != bytes(&quot;!quit&quot;, &quot;utf8&quot;):
                  broadcast(msg, name + &quot;: &quot;)
              else:
                  client.send(bytes(&quot;!quit&quot;, &quot;utf8&quot;))
                  client.close()
                  del clients[client]
                  broadcast(bytes(&quot;%s has left the chat.&quot; % name, &quot;utf8&quot;))
                  break
  
  
  def broadcast(msg, prefix=&quot;&quot;):  # prefix is for name identification.
      &quot;&quot;&quot;Broadcasts a message to all the clients.&quot;&quot;&quot;
      for sock in clients:
          sock.send(bytes(prefix, &quot;utf8&quot;)+msg)
  
          
  clients = {}
  addresses = {}
  
  HOST = gethostbyname(gethostname())
  while True:
      try:
          PORT = int(input(&quot;Enter port to run server on: &quot;))
          if PORT &gt; 1024:
              break
          print(&quot;Invalid port entered\n&quot;)
      except:
          print('Port must be a number &gt; 1024\n')
  print(&quot;\nServer running on host %s, and on port %s&quot; % (HOST, PORT,))
  BUFSIZ = 1024
  ADDR = (HOST, PORT)
  
  SERVER = socket(AF_INET, SOCK_STREAM)
  SERVER.bind(ADDR)
  
  if __name__ == &quot;__main__&quot;:
      SERVER.listen(5)
      print(&quot;Waiting for server connection...&quot;)
      ACCEPT_THREAD = Thread(target=accept_incoming_connections)
      ACCEPT_THREAD.start()
      ACCEPT_THREAD.join()
      SERVER.close()
  </code></pre>
  <p>And the client:</p>
  <pre><code>from socket import AF_INET, socket, SOCK_STREAM
  from threading import Thread
  import tkinter
  
  
  def receive():
      &quot;&quot;&quot;Handles receiving of messages.&quot;&quot;&quot;
      while True:
          try:
              msg = client_socket.recv(BUFSIZ).decode(&quot;utf8&quot;)
              msg_list.insert(tkinter.END, msg)
          except OSError:  # Possibly client has left the chat.
              break
  
  
  def send(event=None):  # event is passed by binders.
      &quot;&quot;&quot;Handles sending of messages.&quot;&quot;&quot;
      msg = my_msg.get()
      my_msg.set(&quot;&quot;)  # Clears input field.
      client_socket.send(bytes(msg, &quot;utf8&quot;))
      if msg == &quot;!quit&quot;:
          client_socket.close()
          top.quit()
  
  
  def on_closing(event=None):
      &quot;&quot;&quot;This function is to be called when the window is closed.&quot;&quot;&quot;
      my_msg.set(&quot;!quit&quot;)
      send()
  
  
  HOST = input('Enter host: ')
  PORT = input('Enter port: ')
  if not PORT:
      PORT = 12345
  else:
      PORT = int(PORT)
  
  try:
      BUFSIZ = 1024
      ADDR = (HOST, PORT)
  
      client_socket = socket(AF_INET, SOCK_STREAM)
      client_socket.connect(ADDR)
  except:
      print(&quot;Couldn't connect - the server may not be up or you had a different problem.&quot;)
      exit()
  
  
  top = tkinter.Tk()
  top.title(&quot;Chatroom&quot;)
  
  messages_frame = tkinter.Frame(top)
  my_msg = tkinter.StringVar()  # For the messages to be sent.
  scrollbar = tkinter.Scrollbar(messages_frame)  # To navigate through past messages.
  # Following will contain the messages.
  msg_list = tkinter.Listbox(messages_frame, height=15, width=50, yscrollcommand=scrollbar.set)
  scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
  msg_list.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
  msg_list.pack()
  messages_frame.pack()
  
  entry_field = tkinter.Entry(top, textvariable=my_msg)
  entry_field.bind(&quot;&lt;Return&gt;&quot;, send)
  entry_field.pack()
  send_button = tkinter.Button(top, text=&quot;Send&quot;, command=send)
  send_button.pack()
  
  top.protocol(&quot;WM_DELETE_WINDOW&quot;, on_closing)
  
  receive_thread = Thread(target=receive)
  receive_thread.start()
  tkinter.mainloop()  # Starts GUI execution.
  </code></pre>