提问人:Ralf_Reddings 提问时间:11/11/2023 更新时间:11/11/2023 访问量:54
我的简单 python 程序不执行最后一行
My simple python program does not execute the last line
问:
我有一个简单的Firefox扩展,我已经大致弄清楚了Firefox方面的东西(JavaScript,DOM和启动Python程序)。
解释一切应该如何工作:
- 浏览器中发生事件
- Firefox 在本地系统上启动 Python 程序(通过 Native Messaging 实现)
- Firefox 通过 stdin 向 Python 程序传递一次性消息
在第 3 步之后,Firefox 将退出图片,而 Python 应该接管。
我被困在这个过程的 Python 部分。python 程序确实通过 stdin 从 Firefox 接收消息。但是一旦执行越过了界限,我就开始变得奇怪。例如,最后一行永远不会执行。即使我手动启动 Python 程序,比如在文件资源管理器中双击它,最后一行也永远不会执行。receivedMessage = getMessage()
subprocess.Popen(...
使其执行的唯一方法是注释掉 .receivedMessage = getMessage()
import subprocess
import json
import struct
import sys
def getMessage():
rawLength = sys.stdin.buffer.read(4)
messageLength = struct.unpack('@I', rawLength)[0]
message = sys.stdin.buffer.read(messageLength).decode('utf-8')
return json.loads(message)
receivedMessage = getMessage()
#subprocess.Popen(["explorer", "C:/Temp"]) #Is never executed
subprocess.Popen(['pwsh', 'C:/Temp/testProg.ps1']) #Is never executed
该程序的核心是我从 MDN 文档页面获得的一个示例,我通过删除冗余部分对其进行了重新设计。我不知道 stdin 背后的技术细节以及它是如何在 Python 中具体实现的,我只在高层次上理解它。
是什么阻碍了程序的执行?它会不会被Firefox仍然向它流式传输数据所阻碍?
任何帮助将不胜感激!
答: 暂无答案
评论
sys.stdin.buffer.read()