How can I communicate between running python code and nodejs
I would like to run python code and communicate with an expressjs express node. So far, I can get my nodejs server to call python functions through one of two mechanisms to invoke a python task or talk to the zerorpc python server.
First, a la http://www.sohamkamani.com/blog/2015/08/21/python-nodejs-comm/ , this works:
var express = require( "express" );
var http = require( "http" );
var app = express();
var server = http.createServer( app ).listen( 3000 );
var io = require( "socket.io" )( server );
app.use( express.static( "./public" ) );
io.on( "connection", function( socket ) {
// Repeat interval is in milliseconds
setInterval( function() {
var spawn = require( 'child_process' ).spawn,
py = spawn( 'python', [ 'mytime.py' ] ),
message = '';
py.stdout.on( 'data', function( data ) {
message += data.toString();
});
py.stdout.on( 'end', function() {
socket.emit( "message", message );
});
}, 50 );
});
Where is mytime.py
from datetime import datetime
import sys
def main():
now = datetime.now()
sys.stdout.write( now.strftime( "%-d %b %Y %H:%M:%S.%f" ) )
And from zerorpc http://www.zerorpc.io/ if this python code is running:
from datetime import datetime
import sys
import zerorpc
class MyTime( object ):
def gettime( self ):
now = datetime.now()
return now.strftime( "%-d %b %Y %H:%M:%S.%f" )
s = zerorpc.Server( MyTime() )
s.bind( "tcp://0.0.0.0:4242" )
s.run()
This nodejs code works:
var express = require( "express" );
var http = require( "http" );
var app = express();
var server = http.createServer( app ).listen( 3000 );
var io = require( "socket.io" )( server );
var zerorpc = require( "zerorpc" );
var client = new zerorpc.Client();
client.connect( "tcp://127.0.0.1:4242" );
app.use( express.static( "./public" ) );
io.on( "connection", function( socket ) {
// Repeat interval is in milliseconds
setInterval( function() {
client.invoke( "gettime", function( error, res, more ) {
socket.emit( "message", res.toString( 'utf8' ) );
} );
}, 50 );
});
But what I would like to do, instead of just calling the python functions, I would like to start a separate python process and send messages to a nodejs server, which listens for them and then processes them. I experimented with the standard socket pattern, but if I try to set up a python server with zerorpc on the same port as the nodejs express server, it gives zmq.error.ZMQError: Address an already used error.
I know I am not thinking about this right - I know that I am missing some logic of inter-process communication due to my naivety here - so if there is a better way to make sending messages from a python process using nodejs while listening to the server, I am all ears ...
Any ideas?
Thanks in advance!
source to share
For those trying to figure it out, here is the solution thanks to Zeke Alexandre Nierenberg
For node.js server code:
var express = require( "express" );
var app = express();
var http = require( "http" );
app.use( express.static( "./public" ) ); // where the web page code goes
var http_server = http.createServer( app ).listen( 3000 );
var http_io = require( "socket.io" )( http_server );
http_io.on( "connection", function( httpsocket ) {
httpsocket.on( 'python-message', function( fromPython ) {
httpsocket.broadcast.emit( 'message', fromPython );
});
});
and the python code that sends messages to it:
from datetime import datetime
from socketIO_client import SocketIO, LoggingNamespace
import sys
while True:
with SocketIO( 'localhost', 3000, LoggingNamespace ) as socketIO:
now = datetime.now()
socketIO.emit( 'python-message', now.strftime( "%-d %b %Y %H:%M:%S.%f" ) )
socketIO.wait( seconds=1 )
Voila!
source to share
I'm having problems with socketIO version ...
so, this is my solution:
NodeJS:
var app = require("express")();
var http = require('http').Server(app);
var bodyParser = require('body-parser');
app.use(bodyParser.json())
app.post('/',function(req,res){
var msg=req.body.msg;
console.log("python: " + msg);
});
http.listen(3000, function(){
console.log('listening...');
});
in Python:
import requests
import json
url = "http://localhost:3000"
data = {'msg': 'Hi!!!'}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(data), headers=headers)
source to share