Skip to content Skip to sidebar Skip to footer

Connect JS Client With Python Server

I'm relatively new to JS and Python, so this is probably a beginners question. I'm trying to send a string from a JS client to Python Server (and then send the string to another Py

Solution 1:

If you want to connect to the python TCP server, then you can use a code similar to the following in node:

var net = require('net');

var client = new net.Socket();
client.connect(8484, '127.0.0.1', function() {
    console.log('Connected');
    client.write('Hello, server! Love, Client.');
});

client.on('data', function(data) {
    console.log('Received: ' + data);
    client.destroy(); // kill client after server's response
});

The server started by your python code doesn't listens for raw TCP connections. If you read up on HTTP protocol you will understand why you saw those strings when your js code sent that string.


Solution 2:

socket.socket() doesn't create a websocket server.

You should use a websocket server, check this : https://websockets.readthedocs.io/en/stable/

Also your python client should use websockets too, even though you could still send data using raw sockets.


Post a Comment for "Connect JS Client With Python Server"