How to communicate between Nodejs and Python with socket.io

Do you want to communicate between nodejs and python this post is for you , this tutorial very easy

This link contain github codes

 

Python Code

import socket
import sys
import time
import datetime
import sys

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_address = ('nodejs_server_ip_adress', 4545)
print >>sys.stderr, 'connecting to %s port %s' % server_address
sock.connect(server_address)

sock.sendall("start")

while True:
    veri="deneme"
    veriler = ("%s,%s"%(veri,str(datetime.datetime.now())))
 
    print(veriler)
    sock.sendall(veriler)

    time.sleep(5)

Nodejs Code

var net = require('net');
var JsonSocket = require('json-socket');
var port1 = 4545;
var server = net.createServer();

server.listen(port1);
server.on('connection', function(socket) {
    liste=[];
    object={};
    liste.push(socket.remoteAddress);
    console.log(liste);
    socket = new JsonSocket(socket);
    var n;
    var isRunning = false;
    var streatTimeout;
    
    socket.on('data', function(data) {
        var str= data.toString();
        console.log("veri : "+data.toString());    
        var array = str.split(',');
        console.log(array);
        io.emit(array[0],array)
    });
});

 

Facebook Comments
News Reporter

Leave a Reply

Your email address will not be published. Required fields are marked *