【问题标题】:Pass flags to Python Script from Node/Express app python-shell从 Node/Express 应用程序 python-shell 将标志传递给 Python 脚本
【发布时间】:2016-04-19 18:16:11
【问题描述】:

我试图弄清楚如何将标志从 Node/Express 应用程序传递给 python 脚本。在命令行中,我通过运行来执行脚本:

python ndbc.py --buoy 46232

我正在使用 python-shell 模块,我认为它应该允许我这样做,但我不完全确定它是如何工作的。

python-shell 文档:

https://github.com/extrabacon/python-shell#running-a-python-script-with-arguments-and-options

【问题讨论】:

    标签: javascript python node.js express


    【解决方案1】:

    改编自自述文件,这样的东西应该可以工作:

    var PythonShell = require('python-shell');
    
    var options = {
      args: ['--buoy', '46232']
    };
    
    PythonShell.run('ndbc.py', options, function (err, results) {
      if (err) throw err;
      console.log('results: %j', results);
    });
    

    【讨论】:

    • 谢谢!我会试一试,只是为了更好地理解 pythonShell 模块。在与我一起从事该项目的一位朋友的帮助下,我们实际上提出了另一种可行的解决方案。我会在这里发布。
    【解决方案2】:

    我找到了另一个使用 child_process 模块的解决方案:

    var exec = require('child_process').exec;
    var pyArgs = {
      // make arguments that take no parameters (ie, --json) true or false
      "buoy": '46232',
      "datasource": 'http',
      "json": true,
      "datatype": "spectra",
      "units": 'ft'
    };
    //example
    pyArgs.datatype = '9band';
    
    function flagGen(args) {
      var flags = '';
      for (var a in args) {
        if (args.hasOwnProperty(a)) {
          if (typeof(pyArgs[a]) == 'string'){
            flags += " --" + a + ' ' + pyArgs[a];
          }
          else {
            if (pyArgs[a] == true)
              flags += ' --' + a;
          }
        }
      }
      return flags;
    }
    
    var pyPath = './';
    var buoyData = ''
    var execstr = 'python ' + path.join(pyPath, 'ndbc.py') + flagGen(pyArgs);
    var child = exec(execstr, function(error, stdout, stderr) {
      if (error) {
        console.log(stderr)
      }
      else {
        buoyData= JSON.parse(stdout);
        console.log(buoyData);
      }
    });
    

    【讨论】:

      【解决方案3】:

      测试:

      const PyShell = require("python-shell");
      
      let options = {
          mode: 'text',
          pythonPath: 'your_python_path',
          pythonOptions: ['-u'], // get print results in real-time
          args: ['-p {"a":1, "b":"123"}']
      };
      
      let pyshell = new PyShell.PythonShell('your_script_path', options);
      
      pyshell.on('message', function(message) {
          // received a message sent from the Python script (a simple "print" statement)
          console.log("Received", message);
      });
      
      // end the input stream and allow the process to exit
      pyshell.end(function(err, code, signal) {
          if (err) throw err;
          console.log('The exit code was: ' + code);
          console.log('The exit signal was: ' + signal);
          console.log('finished');
      });

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-07
        • 1970-01-01
        • 1970-01-01
        • 2010-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-02
        相关资源
        最近更新 更多