title: WebSocket javascript client and python server. Retrieving garbage in output
body: <p>I am trying to send string/text data from browser client to python server and simply print it out. I have followed several examples on the internet, and all are the same: by using <code>javascript</code></p>
  
  <pre><code>web_socket.send("text to be sent") 
  </code></pre>
  
  <p>and (python)</p>
  
  <pre><code>data = web_socket.recv(1024)
  print data 
  </code></pre>
  
  <p>they receive what they want, what is clear and nice printout "text to be sent" on server site.</p>
  
  <p>You can find my <code>.html</code> and <code>.py</code> below:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;title&gt;Test&lt;/title&gt;
      &lt;script src="jquery.js"&gt;&lt;/script&gt;  
      &lt;script type="application/javascript"&gt;
          var ws;
  
          function init() {
              var servermsg = document.getElementById("servermsg");
              ws = new WebSocket("ws://127.0.0.1:9877/");
              ws.onopen = function(){
                  servermsg.innerHTML = servermsg.innerHTML + "&lt;br&gt;Server connected";
              };
              ws.onmessage = function(e){
                  servermsg.innerHTML = servermsg.innerHTML + "&lt;br&gt;&lt;&lt; Recieved data: " + e.data;
              };
              ws.onclose = function(){
                  servermsg.innerHTML = servermsg.innerHTML + "&lt;br&gt;Server disconnected";
              };
          }
          function postmsg(){
              var text = document.getElementById("message").value;
              ws.send(text);
              servermsg.innerHTML = servermsg.innerHTML + "&lt;br&gt;&gt;&gt; Data sent: " + text;
          }
          //$(function(){
          //    var text = document.getElementById("message").value;
          //    ws.send(text);
          //    servermsg.innerHTML = servermsg.innerHTML + "&lt;br&gt;Sent: " + text;            
          //});
  
  
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body onload="init();"&gt;
      &lt;form action="" onSubmit="postmsg();return false;"&gt;
          &lt;input type="text" name="message" value="" id="message"&gt;
          &lt;input type="submit" name="submit" value="" id="submit"&gt;
      &lt;/form&gt;
      &lt;div id="servermsg"&gt;&lt;h1&gt;Message log:&lt;/h1&gt;&lt;/div&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>#!/usr/bin/env python
  
  import socket
  import threading
  import struct
  import hashlib
  import base64
  
  PORT = 9877
  _address = ""
  
  def create_handshake_resp(handshake):
  final_line = ""
  lines = handshake.splitlines()
  for line in lines:
      parts = line.partition(": ")
      if parts[0] == "Sec-WebSocket-Key":
          key = parts[2]
  
  
  magic = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
  
  accept_key = base64.b64encode(hashlib.sha1(key+magic).digest())
  
  return (
      "HTTP/1.1 101 Switching Protocols\r\n"
      "Upgrade: WebSocket\r\n"
      "Connection: Upgrade\r\n"
      "Sec-WebSocket-Accept: " + accept_key + "\r\n\r\n")
  
  
  def handle(s, addr):
  data = s.recv(1024)
  response = create_handshake_resp(data)
  s.sendto(response, addr)
  lock = threading.Lock()
  while 1:
      print "Waiting for data from", addr
      data = s.recv(1024)
      print "Done"
      if not data:
          print "No data"
          break
  
      print 'Data from', addr, ':', data
  
  print 'Client closed:', addr
  lock.acquire()
  clients.remove(s)
  lock.release()
  s.close()
  
  def start_server():
  print 'STARTING SERVER...'
  s = socket.socket()
  s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  s.bind(('', PORT))
  s.listen(1)
  print 'SERVER STARTED'
  while 1:
      conn, addr = s.accept()
      print 'NEW CONNECTION ['+str(len(clients))+'], connected by ', addr
      clients.append(conn)
      threading.Thread(target = handle, args = (conn, addr)).start()
  
  clients = []
  start_server()
  </code></pre>
  
  <p>And server printout (when input was like "AA", or "ABC"):</p>
  
  <pre><code>STARTING SERVER...
  SERVER STARTED
  NEW CONNECTION [0], connected by  ('127.0.0.1', 43877)
  Waiting for data from ('127.0.0.1', 43877)
  Done
  Data from ('127.0.0.1', 43877) : ����w�q
  Waiting for data from ('127.0.0.1', 43877)
  Done
  Data from ('127.0.0.1', 43877) : ��)B�h
  Waiting for data from ('127.0.0.1', 43877)
  </code></pre>