How to create a node red node runs local python scripts on a remote python shell?

Hi All,

I've the code below. the python shell is local. If I want to change it to a remote python, how should I do?

const status = require('./status.js')
const {spawn} = require('child_process')

//use 'python3' on linux and 'python' on anything else
const pcmd = process.platform === 'linux' ? 'python3' : 'python'

//initialize child process
const initProc = (node) => {
	if (node.proc == null){
		node.proc = spawn(pcmd, [node.file], ['pipe', 'pipe','pipe'])

		//handle results
		node.proc.stdout.on('data', (data) => {
		  node.status(status.DONE)
			try{
				node.msg.payload = JSON.parse(data.toString())
			}
			catch(err){
				node.msg.payload = data.toString()
			}
			var msg = node.msg
			if(node.wires.length > 1){
				msg = [node.msg, null]
			}
			node.send(msg)
		})

		//handle errors
		node.proc.stderr.on('data', (data) => {
			node.status(status.ERROR)
			try{
				node.msg.payload = JSON.parse(data.toString())
			}
			catch(err){
				node.msg.payload = data.toString()
			}
			var msg = node.msg
			if(node.wires.length > 1){
				msg = [null, node.msg]
			}
			node.send(msg)
		})

		//handle crashes
		node.proc.on('exit', () => {
		  node.proc = null
		})

		//send node configurations to child
		node.proc.stdin.write(JSON.stringify(node.config) + '\n')
	}
}

//send payload as json to python script
const python = (node) => {
	initProc(node)
	node.proc.stdin.write(JSON.stringify(node.msg.payload) + '\n')
}

module.exports = {
	//parse string containing comma separated integers
	listOfInt: (str) => {
		var ints = null
		try{
			ints = str.replace(' ', '').split(',').map((n) => parseInt(n))
			if(ints.some(isNaN)){
				ints = null
			}
		}
		finally{
			return ints
		}
	},

	//initialize node
	run: (RED, node, config) => {
	  RED.nodes.createNode(node, config)
		node.status(status.NONE)

		node.proc = null
		node.msg = {}
		initProc(node)

		//process message
		const handle = (msg) => {
			node.status(status.PROCESSING)
			node.msg = msg
			if(node.topic != undefined){
				node.msg.topic = node.topic
			}
			//send to python child
			python(node)
		}

		//handle input
		node.on('input', (msg) => {
			//if the node requires preprocessing of message, call preMsg
			if(node.preMsg != undefined){
				node.preMsg(msg, handle)
			}
			else{
				handle(msg)
			}
		})

		//when node is closed, kill child process
		node.on('close', (done) => {
			node.status(status.NONE)
			if(node.proc != null){
				node.proc.kill()
				node.proc = null
			}
			done()
    })
	}
}

Welcome to the forum @Dunton

Do you mean that you want to run a python script on a different computer to the one running node red? If so then the easiest might be to run node red on that computer too.
A good way to communicate between the two node red instances is to use MQTT.

Another way would be to run it via a remote SSH shell. If you want to do that then I suggest researching how to do that first, before implementing it in node red.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.