- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,情况是这样的:我正在使用 Python 和 wx (wxphoenix) 构建一个 macOS GUI 应用程序。用户可以使用 GUI(例如:script1
)启动文件删除过程(包含在 script2
中)。为了成功运行 script2
需要以 sudo 权限运行。
script2
将遍历一长串文件并将其删除。但我需要它在每一轮之后与 script1
中包含的 GUI 进行通信,以便 script1
可以更新进度条。
在绝对最基本的形式中,我的当前工作设置如下所示:
脚本 1:
import io
from threading import Thread
import subprocess
import wx
# a whole lot of wx GUI stuff
def get_password():
"""Retrieve user password via a GUI"""
# A wx solution using wx.PasswordEntryDialog()
# Store password in a variable
return variable
class run_script_with_sudo(Thread):
"""Launch a script with administrator privileges"""
def __init__(self, path_to_script, wx_pubsub_sendmessage):
"""Set variables to self"""
self.path = path_to_script
self.sender = wx_pubsub_sendmessage
self.password = get_password()
Thread.__init__(self)
self.start()
def run(self):
"""Run thread"""
prepare_script = subprocess.Popen(["echo", password], stdout=subprocess.PIPE)
prepare_script.wait()
launch_script = subprocess.Popen(['sudo', '-S', '/usr/local/bin/python3.6', '-u', self.path], stdin=prepare_script.stdout, stdout=subprocess.PIPE)
for line in io.TextIOWrapper(launch_script.stdout, encoding="utf-8"):
print("Received line: ", line.rstrip())
# Tell progressbar to add another step:
wx.CallAfter(self.sender, "update", msg="")
脚本 2:
import time
# This is a test setup, just a very simple loop that produces an output.
for i in range(25):
time.sleep(1)
print(i)
上述设置的工作原理是 script1
实时接收 script2
的输出并对其进行操作。 (所以在给定的示例中:script1
每秒向进度条添加一个步骤,直到它达到 25 个步骤)。
我想要实现的目标 = 不将密码存储在变量中并使用 macOS 它的 native GUI 来检索密码。
但是当我改变时:
prepare_script = subprocess.Popen(["echo", password], stdout=subprocess.PIPE)
prepare_script.wait()
launch_script = subprocess.Popen(['sudo', '-S', '/usr/local/bin/python3.6', '-u', self.path], stdin=prepare_script.stdout, stdout=subprocess.PIPE)
for line in io.TextIOWrapper(launch_script.stdout, encoding="utf-8"):
print("Received line: ", line.rstrip())
# Tell progressbar to add another step:
wx.CallAfter(self.sender, "update", msg="")
进入:
command = r"""/usr/bin/osascript -e 'do shell script "/usr/local/bin/python3.6 -u """ + self.path + """ with prompt "Sart Deletion Process " with administrator privileges'"""
command_list = shlex.split(command)
launch_script = subprocess.Popen(command_list, stdout=subprocess.PIPE)
for line in io.TextIOWrapper(launch_script.stdout, encoding="utf-8"):
print("Received line: ", line.rstrip())
# Tell progressbar to add another step:
wx.CallAfter(self.sender, "update", msg="")
它停止工作因为 osascript 显然 runs in a non-interactive shell .这意味着 script2
在完全完成之前不会发送任何输出,从而导致 script1
中的进度条停止。
我的问题因此变成了:我怎样才能确保使用 macOS native GUI 来询问 sudo 密码,从而避免必须将其存储在变量中,同时仍然保持捕获的可能性来自交互式/实时流中特权脚本的标准输出。
希望这是有道理的。
非常感谢任何见解!
最佳答案
My question thus becomes: How can I make sure to use macOS native GUI to ask for the sudo password, thus preventing having to store it in a variable, while still maintaining the possibility to catch the stdout from the privileged script in an interactive / real-time stream.
我自己找到了一个解决方案,使用命名管道 (os.mkfifo()
)。
这样,您可以让 2 个 python 脚本相互通信,同时其中 1 个通过 osascript 以特权权限启动(意思是:您获得一个 native GUI 窗口,要求用户输入 sudo 密码)。
工作解决方案:
mainscript.py
import os
from pathlib import Path
import shlex
import subprocess
import sys
from threading import Thread
import time
class LaunchDeletionProcess(Thread):
def __init__(self):
Thread.__init__(self)
def run(self):
launch_command = r"""/usr/bin/osascript -e 'do shell script "/usr/local/bin/python3.6 -u /path/to/priviliged_script.py" with prompt "Sart Deletion Process " with administrator privileges'"""
split_command = shlex.split(launch_command)
print("Thread 1 started")
testprogram = subprocess.Popen(split_command)
testprogram.wait()
print("Thread1 Finished")
class ReadStatus(Thread):
def __init__(self):
Thread.__init__(self)
def run(self):
while not os.path.exists(os.path.expanduser("~/p1")):
time.sleep(0.1)
print("Thread 2 started")
self.wfPath = os.path.expanduser("~/p1")
rp = open(self.wfPath, 'r')
response = rp.read()
self.try_pipe(response)
def try_pipe(self, response):
rp = open(self.wfPath, 'r')
response = rp.read()
print("Receiving response: ", response)
rp.close()
if response == str(self.nr_of_steps-1):
print("Got to end")
os.remove(os.path.expanduser("~/p1"))
else:
time.sleep(1)
self.try_pipe(response)
if __name__ == "__main__":
thread1 = LaunchDeletionProcess()
thread2 = ReadStatus()
thread1.start()
thread2.start()
priviliged_script.py
import os
import time
import random
wfPath = os.path.expanduser("~/p1")
try:
os.mkfifo(wfPath)
except OSError:
print("error")
pass
result = 10
nr = 0
while nr < result:
random_nr = random.random()
wp = open(wfPath, 'w')
print("writing new number: ", random_nr)
wp.write("Number: " + str(random_nr))
wp.close()
time.sleep(1)
nr += 1
wp = open(wfPath, 'w')
wp.write("end")
wp.close()
关于python - 如何以及在何处通过基于 macOS Python 的应用程序上的 native GUI 最好地检索 sudo 密码 - (同时维护交互式输出流(stdout)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49171769/
我正在开发一个使用 JTable 的简单 Java 应用程序。我正在尝试实现我所看到的here 。给定链接上的表格的行为与我在表格中想要的行为完全相同。我已完全按照引用链接上显示的方式进行操作。 但是
我需要将 *.ml 文件加载到 Ocaml 顶层(交互式解释器,当您在 shell 中键入“ocaml”时),然后从 Matlab 进程发送指令,获取指令结果,发送返回另一条指令,... 我编写了这个
我正在尝试实现交互式 UILocalNotifications。 以下是我的代码。我无法获得接收通知的 3 个操作按钮。 UIMutableUserNotificationAction *nActio
我不是一个干净的 CSS 编码器,所以这可能是我问题的症结所在……但是……我从本教程中拼凑了一张交互式 map :http://www.noobcube.com/tutorials/html-css/
这个问题在这里已经有了答案: 关闭 13 年前。 Possible Duplicate: How to save a Python interactive session? 我可以在“头脑 Stor
我试图在不扩展 ListActivity 类的情况下创建交互式 ListView。布局应该是左侧的 RatingBar 和标签向右。这是我目前的代码,没有任何编译错误,但在应用程序启动时崩溃: pub
我正在尝试使用 antlr 编写一种简单的交互式(使用 System.in 作为源)语言,但我遇到了一些问题。我在网上找到的例子都是使用每行循环,例如: while(readline) resul
我想创建一个交互式 JTable。为此,我想在表格的单元格中添加 JPanel。一旦 JPanel 位于单元格中,我就可以将我的各种组件添加到 JPanel 中,从而使表格具有交互性。每个 JPane
我有两个具有完全相同文件的分支(如果您想知道它是一个 .sql 文件),我想以交互方式 merge 它。 我非常想像在发生冲突(或命令行)时那样打开一个 diff 程序,然后准确选择行到哪里。 有什么
我想用交互式 rebase 编辑一个提交。当我用 edit 替换 pick 时,此提交的更改仍应用于 repo(以便我可以编辑提交)但我想从头开始重写它。我该怎么做? 最佳答案 在交互式 rebase
来自 Python relative imports for the billionth time : 要使 from .. import 起作用,模块名称中的点数必须至少与 import 语句中的点
如何使条形与一整天一样宽?现在,条形图更像是划分一天的开始的线。我是否从条形标记切换到矩形标记?我会玩带秤吗?或者我是否在不修改数据的情况下强制分箱? 现在条形图太细了。因为图形是交互式的,所以我无法
是否有任何命令行技巧可以让 SVN 添加来自 svn stat 的所有丢失的文件?互动? 例如,类似于: svn add --interactive $ new file: file1.tmp
我想知道这是否可以(可能不是)使用 R 中的并行处理后端之一来完成。 .我尝试了一些谷歌搜索,但一无所获。 我目前遇到的一般问题: 我有一些大物体需要大约半小时才能到达 load 我想在数据上生成一系
我一直在关注问题 here 的建议暂时将各种提示/信息放在我在 Altair 中的地 block 上。但是,如果 Altair 绘图设置为 interactive(),则此建议不起作用 - 在我看来,
简短的问题是,如果 shell 位于不拥有 tty 的孤立进程组中,它应该做什么?但我建议阅读长问题,因为它很有趣。 这是一种有趣且令人兴奋的方法,可以使用您最喜欢的 shell 将您的笔记本电脑变成
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
有没有办法实现交互式 cli,允许您从多个选项中进行选择? 我想要实现如下目标: 显示的示例是使用 JavaScript 中的 Quirer.js 库实现的,但我似乎找不到在 Java 中实现类似功能
我正在尝试使用运行一些命令的输入脚本来运行交互式 R (Windows XP),然后让我进入 R 命令行提示符。但是,当我运行它时,它会退出。 例如,这是输入文件: test.r: x = 1 x 以
我是一名优秀的程序员,十分优秀!