-1

I need to build a website in which Javascript is used as the client language to communicate with a python server over websockets. The website needs to be hosted on Linux. For now, I'm using command python -m SimpleHTTPServer to build a simple server, and the server is also on the Linux and using python.

How do I go about at setting up the javascript client websocket communication? Or how should I approach the architecture?

2

1 Answer 1

1

Assuming that your python websocket server is up an running. You can connect with it through your website through either html5:

socket= new WebSocket('ws://www.example.com:8000/somesocket');
socket.onopen= function() {
    socket.send('hello');
};
socket.onmessage= function(s) {
    alert('got reply '+s);
};

Or via

http://raw-sockets.sysapps.org/#interface-tcpsocket

https://www.w3.org/TR/tcp-udp-sockets/

navigator.tcpPermission.requestPermission({remoteAddress:"127.0.0.1", remotePort:6789}).then(
  () => {
    // Permission was granted
    // Create a new TCP client socket and connect to remote host
    var mySocket = new TCPSocket("127.0.0.1", 6789);

    // Send data to server
    mySocket.writeable.write("Hello World").then(
        () => {

            // Data sent sucessfully, wait for response
            console.log("Data has been sent to server");
            mySocket.readable.getReader().read().then(
                ({ value, done }) => {
                    if (!done) {
                        // Response received, log it:
                        console.log("Data received from server:" + value);
                    }

                    // Close the TCP connection
                    mySocket.close();
                }
            );
        },
        e => console.error("Sending error: ", e);
    );

Whatever your server looks like, you always need to make sure your server and client use the same "protocol", i.e. they use the same variables to send and order in which to send them.

If you also need to set up a socket server in python, I suggest you look at some tutorials online as this is a non-trivial task: http://www.binarytides.com/python-socket-server-code-example/

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

9 Comments

But I only use the command "python -m SimpleHTTPServer 8000" to host my website on Linux and open the website on Windows by 172.XX.XX.XX:8000. and It seems can't connect.
You should check to see if you don't have a firewall enabled. Does it work if you go to localhost:8080?
BTW,The website can display.
Please clarify. The website shows up but it "can't connect"? So what is the exact command that is giving an issue?
I was under the impressin you wanted to connect through websockets. If you just want to run python code on your computer and output it on a website. You may want to install a XAMPP (or WAMPP) server setup. Check the last answer here: stackoverflow.com/questions/7460938/…
|

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.