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

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

How to close chrome native app made with python?

问题

以下是翻译好的代码部分:

  1. if sys.platform == "win32":
  2. import os, msvcrt
  3. msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
  4. msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
  5. def func():
  6. while True:
  7. if sys.stdin.read(1) == -1:
  8. logging.info("inside func got minus one")
  9. sys.exit()
  10. p = multiprocessing.Process(target=func)
  11. 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 -

  1. if sys.platform == "win32":
  2. import os, msvcrt
  3. msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
  4. msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
  5. def func():
  6. while True:
  7. if sys.stdin.read(1) == -1:
  8. logging.info("inside func got minus one")
  9. sys.exit()
  10. p = multiprocessing.Process(target=func)
  11. p.start()

答案1

得分: 1

你应该查看https://developer.chrome.com/apps/nativeMessaging,其中包括一个Python示例。
你需要读取消息的长度。它是一个4字节(32位)整数。如果长度为0,那就是你退出的信号。

  1. def func():
  2. while True:
  3. text_length_bytes = sys.stdin.read(4)
  4. if len(text_length_bytes) == 0:
  5. sys.exit(0)
  6. # 仍在此处,从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.

  1. def func():
  2. while True:
  3. text_length_bytes = sys.stdin.read(4)
  4. if len(text_length_bytes) == 0:
  5. sys.exit(0)
  6. # still here, read text_lenth_bytes from stdin
  7. </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:

确定