title: Websocket Secure using Javascript client and Python server
body: <p>I have embarked on a learning quest about sockets, particularly websockets. I wish to use a php website as a front end, while using a regular python script (not via CGI) as my server side script. I wish to use https for my webpage and the secure version of websockets (wss) but can't seem to find anything that works. Could someone point me in the right direction?</p>
  
  <p>Here is my Javascript client:</p>
  
  <pre><code>ws = new WebSocket('wss://192.168.2.150:8000/');
  ws.onmessage = function(event){document.write("message received"); alert(event.data); ws.close();}
  ws.onopen = function(){console.log("connection is established");}
  ws.onerror = function(event) {
      console.error("ws fail:", event);
  }
  </code></pre>
  
  <p>The webpage which this javascript is running on is being accessed by my desktop at 192.168.2.135. </p>
  
  <p>The same computer that is running the apache2 server is also running my python script. This python script is hosting the websockets server. It is from Dave P (<a href="https://github.com/dpallot/simple-websocket-server" rel="nofollow noreferrer">https://github.com/dpallot/simple-websocket-server</a>) but i have modified it slightly. Here's that code:</p>
  
  <pre><code>
  import signal
  import sys
  import ssl
  from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer, SimpleSSLWebSocketServer
  from optparse import OptionParser
  
  class SimpleEcho(WebSocket):
  
     def handleMessage(self):
        self.sendMessage(self.data)
  
     def handleConnected(self):
        pass
  
     def handleClose(self):
        pass
  
  clients = []
  
  
  if __name__ == "__main__":
  
     parser = OptionParser(usage="usage: %prog [options]", version="%prog 1.0")
     parser.add_option("--host", default='192.168.2.150', type='string', action="store", dest="host", help="hostname (192.168.2.150)")
     parser.add_option("--port", default=8000, type='int', action="store", dest="port", help="port (8000)")
     parser.add_option("--example", default='echo', type='string', action="store", dest="example", help="echo, chat")
     parser.add_option("--ssl", default=1, type='int', action="store", dest="ssl", help="ssl (1: on (default), 0: off")
     parser.add_option("--cert", default='/var/www/html/mainframe/ssl/cacert.pem', type='string', action="store", dest="cert", help="cert (/var/www/html/mainframe/ssl/cacert.pem)")
     parser.add_option("--key", default='/var/www/html/mainframe/ssl/cakey.pem', type='string', action="store", dest="key", help="key (/var/www/html/mainframe/ssl/cakey.pem)")#/etc/ssl/private/apache-selfsigned.key
     parser.add_option("--ver", default=ssl.PROTOCOL_TLSv1, type=int, action="store", dest="ver", help="ssl version")
  
     (options, args) = parser.parse_args()
  
     cls = SimpleEcho
     if options.example == 'chat':
        cls = SimpleChat
  
     if options.ssl == 1:
        server = SimpleSSLWebSocketServer(options.host, options.port, cls, options.cert, options.key, version=options.ver)
     else:
        server = SimpleWebSocketServer(options.host, options.port, cls)
  
     def close_sig_handler(signal, frame):
        server.close()
        sys.exit()
  
     signal.signal(signal.SIGINT, close_sig_handler)
  
     server.serveforever()
  </code></pre>
  
  <p>Using Wireshark I can see that my client and server do handshake and when the browser send a "Client Hello" the server resonds with a "Server Hello" and key. Then the client sends back its encryption key. But then the browser sends an "Encryption Alert". What does this mean? Has it got anything to do with my self-signed certificate? </p>