2017-07-07 20 views
0

我想發送一些字符串數據從Python3nodeJs。字符串是韓文字符,我編碼到utf8。(原因我不知道其他方式安全地發送數據。)當我發送(從python)它是ByteStream和nodeJs我收到它作爲數組。我將這個數組轉換爲字符串。但現在我無法將字符串解碼爲原始韓文字符。 以下是我正在使用的一些代碼。如何發送編碼的數據從Python到nodejs

蟒蛇

input = sys.argv[1] 
d = bot.get_response(input) 
data = str(d).encode('utf8') 

print(data) 

的NodeJS

var utf = require('utf8'); 
var python = require('python-shell'); 

var pyt = path.normalize('path/to/my/python.exe'), 
     scrp = path.normalize('path/to/my/scriptsFolder/'), 


    var options = { 
     mode: 'text', 
     pythonPath: pyt, 
     pythonOptions: ['-u'], 
     scriptPath: scrp, 
     encoding: 'utf8', 
     args: [message] 
    }; 

    python.run('test.py', options, function (err, results) { 

     //here I need to decode 'results' 
     var originalString = utf.encode(results.toString());// that code is not working for me 

    }); 

我用幾個庫像UTF8解碼,但沒有幫助。 有人可以請給一些想法如何使其工作。

編輯

我有更多的一些信息進行編輯。 我試過了@smarx的方法,但沒有奏效。

我有兩種情況:

如果我發送數據從蟒蛇這裏字符串是我得到的NodeJS b'\xec\x95\x88\xeb\x85\x95\xed\x95\x98\xec\x8b\xad\xeb\x8b\x88\xea\xb9\x8c? \xec\x9d\xb4\xed\x9a\xa8\xec\xa2\x85 \xea\xb3\xa0\xea\xb0\x9d\xeb\x8b\x98! \xeb\x8f\x99\xec\x96\x91\xeb\xa7\xa4\xec\xa7\x81\xec\x9e\x85\xeb\x8b\x88\xeb\x8b\xa4

2.,如果我對數據進行編碼併發送。我得到�ȳ��Ͻʴϱ�? ��ȿ�� ������! �

+0

是什麼'python.run'嗎? – smarx

+0

它運行python腳本。 – mamur

+0

我的意思是比這更具體。 :-)你能分享一下'python.run'的代碼嗎? – smarx

回答

1

我仍然不知道什麼python.run呢,因爲你不會共享代碼,但這裏是我的代碼版本,這是工作的罰款:

test.py

print("안녕 세상") 

app.js

const { exec } = require('child_process'); 

exec('python3 test.py', function (err, stdout, stderr) { 
    console.log(stdout); 
}); 

// Output: 
// 안녕 세상 
+0

我已經嘗試過,但似乎不工作。 – mamur