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

**URL:** <https://discourse.nodered.org/t/how-to-create-a-node-red-node-runs-local-python-scripts-on-a-remote-python-shell/71230>\
**Category:** General\
**Tags:** http-request, function-node, mqtt\
**Created:** [25 November 2022 07:29 UTC](https://discourse.nodered.org/t/how-to-create-a-node-red-node-runs-local-python-scripts-on-a-remote-python-shell/71230 "2022-11-25T07:29:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dunton](https://avatars.discourse-cdn.com/v4/letter/d/a6a055/32.png) [@Dunton](https://discourse.nodered.org/u/Dunton)\
**Post date:** [25 November 2022 07:29 UTC](https://discourse.nodered.org/t/how-to-create-a-node-red-node-runs-local-python-scripts-on-a-remote-python-shell/71230/1 "2022-11-25T07:29:14Z")

</div>

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?

```auto
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()
    })
	}
}

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [25 November 2022 07:53 UTC](https://discourse.nodered.org/t/how-to-create-a-node-red-node-runs-local-python-scripts-on-a-remote-python-shell/71230/2 "2022-11-25T07:53:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 January 2023 07:54 UTC](https://discourse.nodered.org/t/how-to-create-a-node-red-node-runs-local-python-scripts-on-a-remote-python-shell/71230/3 "2023-01-24T07:54:30Z")

</div>

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