我一直试图在节点和python之间进行通信,我希望将对象数组发送到python,并在python中打印,但是我的代码不起作用。
content=[
{
"username": "admin",
"first_name": "",
"last_name": "",
"roles": "system_admin system_user",
"locale": "en",
"delete_at": 0,
"update_at": 1511335509393,
"create_at": 1511335500662,
"auth_service": "",
"email": "adminuser@cognizant.com",
"auth_data": "",
"position": "",
"nickname": "",
"id": "pbjds5wmsp8cxr993nmc6ozodh"
},
{
"username": "chatops",
"first_name": "",
"last_name": "",
"roles": "system_user",
"locale": "en",
"delete_at": 0,
"update_at": 1511335743479,
"create_at": 1511335743393,
"auth_service": "",
"email": "chatops@cognizant.com",
"auth_data": "",
"position": "",
"nickname": "",
"id": "akxdddp5p7fjirxq7whhntq1nr"
}]JavaScript代码:
const express=require('express')
const app=express()
let p = require('python-shell');
app.get('/send',(req,res)=>{
var options = {
args:
[
content
]
}
p.PythonShell.run('hello.py', options, function (err, results) {
console.log(results.toString())
});
})
app.listen('5000')Python脚本:
import sys
import json
details=sys.argv[1]
print (details)发布于 2019-12-10 13:43:43
因为我们不能将对象传递给命令行,所以python-shell正在对内容对象调用.toString()方法。让它成为object Object。
解决方案:将options对象更改为:
var options = {
args:
[
JSON.stringify(content)
]
}在python文件中:
parsed = json.loads(sys.argv[1])
print (json.dumps(parsed))https://stackoverflow.com/questions/59237702
复制相似问题