【发布时间】:2017-11-24 06:13:36
【问题描述】:
我有一个应用程序 nodejs,它请求使用 python-shell npm 包发送到 python。和python脚本读取数据并解析字符串。
nodejs 文件:teststr.js
let buf = `<html><body><p>Reply</p></body></html>`
var PythonShell = require('python-shell')
var options = {
mode: 'binary',
args: ['html'],
scriptPath: __dirname + '/',
pythonPath: '/usr/bin/python3'
}
var pyshell = new PythonShell('test.py', options)
pyshell.stdout.on('data', function (message) {
console.log(message)
})
pyshell.send(buf)
pyshell.end(function (err) {
if (err) {
console.log('End Script' + err)
}
})
Python 文件:test.py
import sys
import base64
import struct
import io
type = sys.argv[1]
html = ""
htmlBuffer = ""
for line in sys.stdin.buffer:
htmlBuffer = io.BufferedReader(line)
#htmlBuffer = to_str(htmlBuffer) #htmlBuffer.decode('utf-8', errors='ignore').strip()
print("%s" % (htmlBuffer))
给定的输出如下:
<Buffer 62 27 3c 68 74 6d 6c 3e 3c 62 6f 64 79 3e 3c 70 3e 52 65 70 6c 79 3c 2f 70 3e 3c 2f 62 6f 64 79 3e 3c 2f 68 74 6d 6c 3e 27 0a>
预期结果:
<html><body><p>Reply</p></body></html>
【问题讨论】:
标签: python node.js buffer inputstream