我正在尝试在firefox扩展中使用本机消息。我尝试从此https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging
构建示例我复制/粘贴了所有代码,并按照页面上的说明进行了正确设置,并发送了“ Ping”,但没有收到“ Pong”。浏览器控制台显示TypeError:需要一个类似字节的对象,而不是python应用程序中第17行的“ str”。我该怎么办?
我使用Windows 7和python 3.x Web扩展名将json对象发送到应用程序,然后该应用程序测试长度并对消息进行struct.unpack。如果消息是“ ping”,它将尝试对struct.pack和json.dumps响应“ pong”,网络扩展将其作为响应接收。消息和任何错误都会写入console.log。
在示例中说:
-u
标志的python 我确实设置了.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");
});
python应用程序接收ping并发送pong:
#!/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'
答案 0 :(得分:0)
使用 Python 2 对我来说效果很好(在Linux上)。
#!/usr/bin/python2 -u
或
path\to\python2
分别在Windows批处理文件中。
要使其在 Python 3 中工作,您需要将编码后的字符串打包到一个结构中(我不知道为什么-但它可以工作):
#!/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():
# use buffer to get bytes
raw_length = sys.stdin.buffer.read(4)
#raise ValueError(raw_length)
if not raw_length:
sys.exit(0)
message_length = struct.unpack('=I', raw_length)[0]
message = sys.stdin.buffer.read(message_length).decode("utf-8")
return json.loads(message)
# Encode a message for transmission, given its content.
def encode_message(message_content):
encoded_content = json.dumps(message_content).encode("utf-8")
encoded_length = struct.pack('=I', len(encoded_content))
return {'length': encoded_length, 'content': struct.pack(str(len(encoded_content))+"s",encoded_content)}
# Send an encoded message to stdout.
def send_message(encoded_message):
#raise ValueError(encoded_message)
sys.stdout.buffer.write(encoded_message['length'])
sys.stdout.buffer.write(encoded_message['content'])
sys.stdout.buffer.flush()
while True:
message = get_message()
if message == "ping":
send_message(encode_message("pong"))