Connect JS client with Python server
<p>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 Python client).</p>

<p>This is my code:</p>

<p>JS client:</p>

<pre><code>    var socket = io.connect('http://127.0.0.1:8484');
    socket.send('lalala');
</code></pre>

<p>Python server:</p>

<pre><code>HOST = '127.0.0.1'
PORT = 8484
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(2)

#JS
conn1, addr1 = s.accept()
print 'Connected by', addr1
#PY
conn2, addr2 = s.accept()
print 'Connected by', addr2

while 1:
    try:
        data = conn1.recv(1024)
    except socket.error:
        print ''
    if data:
        print data.decode('utf-8')
        conn2.send('data')
</code></pre>

<p>Python client:</p>

<pre><code>def __init__(self): #inicializacion
    self.comando = '0'
    self.HOST = '127.0.0.1'
    self.PORT = 8484
    self.cliente = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    fcntl.fcntl(self.cliente, fcntl.F_SETFL, os.O_NONBLOCK)

def activate(self): 
    print "Plugin de envio en marcha."

def deactivate(self):
    print "Plugin de envio off."

def __call__(self, sample):
    self.cliente.connect((self.HOST, self.PORT))
    try:
        self.comando = self.cliente.recv(1024).decode('utf-8')

    except socket.error, e:
        self.comando = '0'

    print "******************"
    print self.comando
    print "******************"
</code></pre>

<p>I have no problem sending a random string from python server to python client, but I can't receive from the JS client. </p>

<p>When I run the server and the clients there are no problems with the connection: </p>

<pre><code>Connected by ('127.0.0.1', 52602)
Connected by ('127.0.0.1', 52603)
</code></pre>

<p>But, for example, this is what I receive from the JS:</p>

<pre><code>GET /socket.io/1/?t=1472322502274 HTTP/1.1
Host: 127.0.0.1:8484
Connection: keep-alive
Origin: http://127.0.0.1:8880
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
Accept: */*
Referer: http://127.0.0.1:8880/normal.html
Accept-Encoding: gzip, deflate, sdch
Accept-Language: es-ES,es;q=0.8
Cookie: io=e7675566ceab4aa1991d55cd72c8075c
</code></pre>

<p>But I want the string 'lalala'.</p>

<p>Any ideas? </p>

<p>(And thank you!)</p>

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

<pre><code>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
});
</code></pre>

<p>The server started by your python code doesn't listens for raw TCP connections. If you read up on <a href="https://tools.ietf.org/html/rfc2616" rel="nofollow">HTTP protocol</a> you will understand why you saw those strings when your js code sent that string. </p>

javascript python node.js sockets socket.io