1

I create a simple echo server and I would like to use it for a simple webserver example.

Until now I have the server:

Server.py

from socket import *
import threading
import thread

def handler(clientsock,addr):
    while 1:
        data = clientsock.recv(BUFSIZ)
        if not data:
            break
        msg = 'echoed:... ' + data
        clientsock.send(msg)
     clientsock.close()

if __name__=='__main__':
    HOST = 'localhost'
    PORT = 21567
    BUFSIZ = 1024
    ADDR = (HOST, PORT)
    serversock = socket(AF_INET, SOCK_STREAM)
    serversock.bind(ADDR)
    serversock.listen(2)

    while 1:
        print 'waiting for connection...'
        clientsock, addr = serversock.accept()
        print '...connected from:', addr
        thread.start_new_thread(handler, (clientsock, addr))

I would like to use websockify and I found that is able to wrap an existing program using:

./websockify 2023 -- PROGRAM ARGS

How can I run the the previous server using the above command? Can I run websockify on one machine while the server.py is running on a different machine? If yes. How can I modify the above command?

Can someone provide a simple HTML5 example to show me how the client.html should look like? Please keep in mind that I want to be able to use the example with browsers that do not have native websocket support. I think websockify is able to do that using web-socket-js.

They state: It has transparent support for binary data and has automatic fallback to the web-socket-js Flash Websocket shim/polyfill if the browser does not support WebSockets natively

1 Answer 1

2

To interface with websockify I recommend that you client HTML/Javascript use the library provided by include/websock.js. The websock.js API is a bit different than standard WebSockets.

I just pushed a very simple HTML client page into the websockify repository.

To wrap your server.py on the same system, you would run websockify like this:

./websockify --web ./ 21567 -- server.py

The load the simple client at "http://localhost:21567/tests/simple.html". Enter "ws://localhost:21567" as the websocket URI and hit connect. Note that the wrap mode uses a rebind.so that you have to build which moves the listen port out of the way so that websockify can interpose for you.

You can also run websockify separately on the same system:

./server.py
./websockify --web ./ 6080 localhost:21567

In that case you'll browse to "http://localhost:6080/tests/simple.html" and use "ws://localhost:6080" as the URI to connect to.

If you want to run websockify on a different machine from the one where server.py is running then you will need to change server.py to listen on a public interface and not just "localhost". Once you make that change, if server.py is running on 192.168.1.7 then you could run websockify on 192.168.1.5 like this:

On 192.168.1.7 launch server.py:

./server.py

On 192.168.1.5 launch websockify and point it at server.py on the other system:

./websockify --web ./ 6080 192.168.1.7:21567

Then you would browse to "http://192.168.1.5:6080/tests/simple.html" and use URI "ws://192.168.1.5:6080".

Alternately, if you are just wanting your server to be a WebSocket server then you can just use the websocket.py library without the full websockify functionality and make your server directly. See the tests/echo.py example included with websockify which is a simple WebSocket echo server.

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

7 Comments

Thanks a lot once again for the extensive answer. Can you please clarify something? If I run server.py on server 1 and websockify on server 2 do I need the rebind.so aswell? I do not think so.
No, you only need rebind.so if you are using it in wrapping mode. Also, I forgot to mention that if you use include/websock.js it will pull in the web-socket-js Flash emulation for browsers without native WebSocket support.
Unfortunately it is not working in IE9. Can you suggest something?
8: 10.16.5.135: Sending flash policy response 9: 10.16.5.135: Plain non-SSL (ws://) WebSocket connection 9: 10.16.5.135: Version hixie-76, base64: 'True'
@salamis, that output indicates that the connection was opened. Can you open a websockify issue so we can track this properly. Also, any output from the javascript console during this would be helpful.
|

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.