3

I have a locally running python websocket (code below) which I let run infinitely. I mainly copied the code from an example. The example uses another python client script to send data to it (also below). That simply works like a charm and is very simple; just what I need since I have some experience in Python but no clue about the 'web/internet'-thingies.

Now, in stead of using the python client, I'd like to send a message from an html document by javascript. Is there any guidance into a short piece of 'stupid' code, just sending one message (string) to the python websocketserver? The python script would then be able to execute different processes using this data.

Python server: (works :) )

from socket import *

host = "localhost"
port = 8089
buf = 1024
addr = (host,port)

sock = socket(AF_INET,SOCK_DGRAM)
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sock.bind(addr)

print "Websocket active."
print "address:\t"+host+":"+str(port)

while 1:
    data,addr = sock.recvfrom(buf)
    if not data:
        print "Client has exited!"
        break
    else:
        print "\nReceived message '", data,"'"

# Close socket
sock.close()

Python Client ( works, but not needed)

from socket import *

host = "localhost"
port = 8089
buf = 1024
addr = (host,port)

sock = socket(AF_INET,SOCK_DGRAM)
def_msg = "===Enter message to send to server===";
print "\n",def_msg

while (1):
    data = raw_input('>> ')
    if not data:
        break
    else:
        if(sock.sendto(data,addr)):
            print "Sending message '",data,"'....."

sock.close()

And now towards the Javascript client..
Can't it be as simple as this? ;

var socket = new WebSocket("ws://localhost:8089");
socket.onopen = function () {
    alert("alerting you");
    socket.send('Pingel');
};

1 Answer 1

3

Your Python server implements a regular TCP server, but your JS code is acting as a WebSocket client, which is an actual proper protocol on top of TCP: https://www.rfc-editor.org/rfc/rfc6455

If you want both ends to communicate properly, you need to run a WebSocket server, like this: https://github.com/dpallot/simple-websocket-server

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

3 Comments

Thanks that's clear, then maybe websockets isn't the way to go? Can I instead of using a websocket server, just pass variables to the Python TCP server?
@WillemvanOpstal if it's your intention to run the JS code in a browser, you're limited to the choice of protocols that you can use (HTTP, HTTPS, WebSockets or WebRTC), which the server has to support.
That makes sense. I indeed want to run the JS in a browser, albeit on a local system. I'll try to get a websocket server running but it doens;t work out for me yet... Thanks for the tips!

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.