0

I tried to implement my first websocket example but I cannot make it work.

I use a python webserver:

import threading
import socket

def start_server():
    tick = 0
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 1234))
    sock.listen(100)
    while True:
        print 'listening...'
        csock, address = sock.accept()
        tick+=1
        print 'connection!' 
        handshake(csock, tick)
        print 'handshaken'
        while True:
            interact(csock, tick)
            tick+=1


def send_data(client, str):
     #_write(request, '\x00' + message.encode('utf-8') + '\xff')
     str = '\x00' + str.encode('utf-8') + '\xff'
     return client.send(str)

def recv_data(client, count):
    data = client.recv(count)    
    return data.decode('utf-8', 'ignore')

def handshake(client, tick):
     our_handshake = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"+"Upgrade:         WebSocket\r\n"+"Connection: Upgrade\r\n"+"WebSocket-Origin: http://localhost:8888\r\n"+"WebSocket-Location: "+" ws://localhost:1234/websession\r\n\r\n"
     shake = recv_data(client, 255)
     print shake
     #We want to send this without any encoding
     client.send(our_handshake)

 def interact(client, tick):
     data = recv_data(client, 255)
     print 'got:%s' %(data)
     send_data(client, "clock ! tick%d" % (tick))
     send_data(client, "out ! %s" %(data))

 if __name__ == '__main__':
     start_server()

And HTML:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Web Socket Example</title>
    <meta charset="UTF-8">
    <script>
      window.onload = function() {
        var s = new WebSocket("ws://localhost:1234/");
        s.onopen = function(e) { s.send('Ping'); }
        s.onmessage = function(e) { alert("got: " + e.data); }
        s.onclose = function(e) { alert("closed"); }
      };
    </script>
    </head>
    <body>
      <div id="holder" style="width:600px; height:300px"></div>
    </body>
 </html>

When I point my browser to http://localhost/websocket.html over apache2

I got the following error:

python websocketserver.py
listening...
connection!
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:1234
Origin: http://localhost
Sec-WebSocket-Key: A4sVkUhjVlTZbJrp2NUrqg==
Sec-WebSocket-Version: 13


handshaken
got:
Traceback (most recent call last):
  File "websocketserver.py", line 43, in <module>
    start_server()
  File "websocketserver.py", line 17, in start_server
    interact(csock, tick)
  File "websocketserver.py", line 40, in interact
    send_data(client, "out ! %s" %(data))
  File "websocketserver.py", line 24, in send_data
    return client.send(str)
socket.error: [Errno 32] Broken pipe

Can someone help me to fix this?

Thanks

2
  • 1
    My suggestion is that do not try to implement WebSocket stack yourself, but use a server and library which supports them out of the box. Some starting points duckduckgo.com/?q=!pypi+websocket Commented Jan 29, 2012 at 13:57
  • Tornado, for instance, has a websocket implementation. Commented Jan 29, 2012 at 15:03

1 Answer 1

3

You're responding with the older Hixie 75 protocol but the client only speaks the newer HyBi/IETF RFC 6455 WebSocket protocol.

Your response to the handshake should look more like this (the accept value is calculated from the key value from the client):

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

In HyBi/6455, the frames are no longer delimited with \x00 and \xff. Instead there is a header to every frame that contains several pieces of data including frame type and payload length.

See the spec for more information. Or better yet, you could refer and/or use an existing python WebSocket implementation such as pywebsocket, tornado, or my own project websockify which contains websocket.py which is a generic websocket server lib.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks mate. I will try to use websockify. I have already checked the VNC that you have built and I will take ideas from there in order to build my own implementation and satisfy my needs. I wanted to email you earlier but I couldn't send you an email through github.
kanaka, where can I find a simple example of websockify with a python server sending images? And a final question. Is websockify working with the flash version of websockets for anyone not having an HTML5 enabled browser?
I don't have a simple example of sending images, but if you send the raw data string using websockify and websock.js you'll get it as a binary string on the other end. Yes, websockify support [web-socket-js](htttp://github.com/kanaka/websockify) and include/websock.js will automatically load it if required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.