本机 (python stdin) 应用程序未收到 Chrome sendNativeMessage (stdout) 输出

Native (python stdin) app not receiving Chrome sendNativeMessage (stdout) output

我正在尝试通过 Chrome sendNativeMessage 函数向我的本机 Python 应用程序发送一些数据,但到目前为止没有成功。

下面是我的Javascript:

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
         var email = {text:request};

         alert(email["text"]) // showing that i successfully received message

         chrome.runtime.sendNativeMessage('com.whatever.whatever.xyz',
         email[0],
          function(response) {
            console.log("Received " + response);
          });

});

下面是我的Python:

import win32com.client
import struct
import sys
import json
import datetime
import time

# On Windows, the default I/O mode is O_TEXT. Set this to O_BINARY
# to avoid unwanted modifications of the input/output streams.
import os, msvcrt
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

# Read the message length (first 4 bytes).
#for line in sys.stdin:
text_length_bytes = sys.stdin.read(4)

# Unpack message length as 4 byte integer.
#text_length = struct.unpack('i', text_length_bytes.encode('utf-8'))[0]
text_length = struct.unpack('i', text_length_bytes)[0]

# Read the text of the message.
#text = sys.stdin.read(text_length)
text = sys.stdin.read(text_length).decode('utf-8')

olMailItem = 0x0

obj = win32com.client.Dispatch("Outlook.Application")

newMail = obj.CreateItem(olMailItem)
newMail.Subject = "Whatever"
newMail.HTMLBody = text

newMail.display()

奇怪的是,我可以通过稍微调整 Python 来成功发送硬编码值(参见注释掉的 "text_length =" 和 "text =" 行)并更改 Javascript 传递 { text: "Hello world!" } 而不是 email[0]。但是当我传递一个对象时,它失败了。

我觉得我在这里缺少一些基本的东西。有人可以帮助我理解吗?

var email = {text:request};

// ...
  email[0]
// ...

真的不是数组吗?