Skip to content

Commit

Permalink
Popen attempts, functional but NOT usable
Browse files Browse the repository at this point in the history
  • Loading branch information
poing committed Feb 14, 2024
1 parent 6d15571 commit fc359af
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 18 deletions.
43 changes: 43 additions & 0 deletions gems/proc_commV1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: set et sw=4 fenc=utf-8:
#
# proc_commV1.py

# Trying to use process.communicate() with Node

import subprocess

#JS_FILE_PATH = "wrapperV7.js"
#js_command = ["node", "-i", JS_FILE_PATH]

# Start the Node.js process
node_process = subprocess.Popen(["node", "-i"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

# Send JavaScript commands
commands = [
"const secrets = require('../secrets.js/secrets.js')",
"console.log('Hello from Node.js!')",
"console.log(2 + 2)",
"console.log(Math.random())",
"var HHGTTG = 42",
"console.log(HHGTTG)",
"secrets.share('1234abc', 6, 3)"
]


# Send commands to the Node.js process
for command in commands:
node_process.stdin.write(command + '\n')

# Read output
output, error = node_process.communicate()

# Close stdin to indicate end of input
node_process.stdin.close()

print("Output:")
print(output)

print("Error:")
print(error)
47 changes: 47 additions & 0 deletions gems/proc_commV2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: set et sw=4 fenc=utf-8:
#
# proc_commV2.py

# Trying to use process.communicate() with Node

import subprocess

#JS_FILE_PATH = "wrapperV7.js"
#js_command = ["node", "-i", JS_FILE_PATH]

# Start the Node.js process
node_process = subprocess.Popen(["node", "-i"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

# Send JavaScript commands
commands = [
"const secrets = require('../secrets.js/secrets.js')",
"console.log('Hello from Node.js!')",
"console.log(2 + 2)",
"console.log(Math.random())",
"var HHGTTG = 42",
"console.log(HHGTTG)",
"secrets.share('1234abc', 6, 3)"
]

#print(commands[0])

# Send commands to the Node.js process
for command in commands:
node_process.stdin.write(command + '\n')
#node_process.stdin.write(commands[1] + '\n')
#foo.stdout.readline()

# Read output
output, error = node_process.communicate()

# Close stdin to indicate end of input
node_process.stdin.close()


print("Output:")
print(output)

print("Error:")
print(error)
36 changes: 18 additions & 18 deletions gems/wrapperV7.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// wrapperV7.js
// Require the secrets package and assign it to a variable
const secrets = require('../secrets.js/secrets.js');

// Function to execute the commands sent from Python
function executeCommands(input_data) {


// Split the input_data into individual commands
const commands = input_data.split(';');

// Execute each command
commands.forEach(command => {
eval(command); // Using eval to execute the command strings
});
}

console.log("Commands received from Python:", process.argv[2]));

// Execute the commands sent from Python
executeCommands(process.argv[2]);
//
// // Function to execute the commands sent from Python
// function executeCommands(input_data) {
//
//
// // Split the input_data into individual commands
// const commands = input_data.split(';');
//
// // Execute each command
// commands.forEach(command => {
// eval(command); // Using eval to execute the command strings
// });
// }
//
// console.log("Commands received from Python:", process.argv[2]));
//
// // Execute the commands sent from Python
// executeCommands(process.argv[2]);

0 comments on commit fc359af

Please sign in to comment.