如何关闭使用Python制作的Chrome本机应用程序?

huangapple go评论83阅读模式
英文:

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>



huangapple
  • 本文由 发表于 2020年1月3日 13:37:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定