英文:
How to close chrome native app made with python?
问题
以下是翻译好的代码部分:
if sys.platform == "win32":
import os, msvcrt
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
def func():
while True:
if sys.stdin.read(1) == -1:
logging.info("inside func got minus one")
sys.exit()
p = multiprocessing.Process(target=func)
p.start()
英文:
I have made an chrome extension that interacts with a native app made with python. On closing my browser my native app doesn't end. I read somewhere that chrome sends -1 as message before ending. Following is the code I used to receive from extension -
if sys.platform == "win32":
import os, msvcrt
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
def func():
while True:
if sys.stdin.read(1) == -1:
logging.info("inside func got minus one")
sys.exit()
p = multiprocessing.Process(target=func)
p.start()
答案1
得分: 1
你应该查看https://developer.chrome.com/apps/nativeMessaging,其中包括一个Python示例。
你需要读取消息的长度。它是一个4字节(32位)整数。如果长度为0,那就是你退出的信号。
def func():
while True:
text_length_bytes = sys.stdin.read(4)
if len(text_length_bytes) == 0:
sys.exit(0)
# 仍在此处,从stdin读取text_length_bytes
英文:
You should have a look at https://developer.chrome.com/apps/nativeMessaging, which includes an example in Python.
You need to read the length of the message. It is a 4 bytes (32 bit) integer. If the length is 0, that is your signal to exit.
def func():
while True:
text_length_bytes = sys.stdin.read(4)
if len(text_length_bytes) == 0:
sys.exit(0)
# still here, read text_lenth_bytes from stdin
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论