Native-Messaging firefox扩展示例“ ping_pong”由于TypeError不起作用

问题描述 投票:0回答:1

我正在尝试在firefox扩展中使用本机消息。我试图从此https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging

构建示例

我复制/粘贴了所有代码,并按照页面上的说明进行了正确设置,并发送了“ Ping”,但没有收到“ Pong”。浏览器控制台显示TypeError:需要一个类似字节的对象,而不是python应用程序中第17行的“ str”。我该怎么办?

我使用Windows 7和python 3.xWeb扩展名将json对象发送到应用程序,然后该应用程序测试长度并对消息进行struct.unpack。如果消息是“ ping”,它将尝试对struct.pack和json.dumps响应“ pong”,网络扩展将其作为响应接收。消息和任何错误都会写入console.log。

在示例中说:

注意,在Windows上需要运行带有-u标志的python,

为了确保以二进制打开stdin和stdout,而是

比文字,模式。

而且我确实将.bat启动脚本设置为包括-u标志,但看来python仍然将stdin读取为字符串,然后在尝试struct.unpack时会给出TypeError。

用于发送ping的网络扩展background.js:

/*
On startup, connect to the "ping_pong" app.
*/
var port = browser.runtime.connectNative("ping_pong");

/*
Listen for messages from the app.
*/
port.onMessage.addListener((response) => {
  console.log("Received: " + response);
});

/*
On a click on the browser action, send the app a message.
*/
browser.browserAction.onClicked.addListener(() => {
  console.log("Sending:  ping");
  port.postMessage("ping");
});

用于接收ping并发送pong的python应用程序:

#!/usr/bin/python -u

# Note that running python with the `-u` flag is required on Windows,
# in order to ensure that stdin and stdout are opened in binary, rather
# than text, mode.

import json
import sys
import struct


# Read a message from stdin and decode it.
def get_message():
    raw_length = sys.stdin.read(4)
    if not raw_length:
        sys.exit(0)
    message_length = struct.unpack('=I', raw_length)[0]
    message = sys.stdin.read(message_length)
    return json.loads(message)


# Encode a message for transmission, given its content.
def encode_message(message_content):
    encoded_content = json.dumps(message_content)
    encoded_length = struct.pack('=I', len(encoded_content))
    return {'length': encoded_length, 'content': encoded_content}


# Send an encoded message to stdout.
def send_message(encoded_message):
    sys.stdout.write(encoded_message['length'])
    sys.stdout.write(encoded_message['content'])
    sys.stdout.flush()


while True:
    message = get_message()
    if message == "ping":
        send_message(encode_message("pong"))

这是给出TypeError的行:

message_length = struct.unpack('=I', raw_length)[0]

日志应显示:发送:ping收到:pong

日志实际上说:发送:ping

stderr output from native app ping_pong: Traceback (most recent call last):
stderr output from native app ping_pong:   File "C:\\Users\\ping_pong\\ping_pong.py", line 37, in <module>
stderr output from native app ping_pong:     message = get_message()
stderr output from native app ping_pong:   File "C:\\Users\\ping_pong\\ping_pong.py", line 17, in get_message
stderr output from native app ping_pong:     message_length = struct.unpack('=I', raw_length)[0]
stderr output from native app ping_pong: TypeError: a bytes-like object is required, not 'str'
json python-3.x firefox-webextensions chrome-native-messaging
1个回答
0
投票
#!/usr/bin/python2 -u

path\to\python2

分别在Windows批处理文件中

我仍在用python 3寻找解决方案。
© www.soinside.com 2019 - 2024. All rights reserved.