私はpython-shell npmパッケージを使用してPythonにリクエストを送信する1つのアプリケーションnodejsを持っています。 Pythonスクリプトはデータを読み込み、文字列を解析します。Nodejs InputStreamバッファをPythonを使用して文字列に変換する
nodejsファイル:
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のファイルteststr.js: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を使用してhtmlを処理し、nodejsに応答を送り返したいと思います。現在、私はPythonのバッファとして入力を受け取りました。どのようにバッファを文字列に変換するのですか? – Chirag
'<バッファ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 0a> ' – Chirag