0

我的程序以 root 权限运行,但我需要一部分以非特权用户身份运行。

基本上, webbrowser.open(SITE) 不能作为 root 工作。除此之外,我的代码需要以 root 身份运行。我尝试了以下方法:

subprocess.call("sudo -u " + getpass.getuser() + " " + webbrowser.open("https://github.com/codywd/WiFiz/issues"))

它会导致以下错误:

Traceback (most recent call last):
  File "main.py", line 364, in OnReport
    subprocess.call("sudo -u " + getpass.getuser() + " " + webbrowser.open("https://github.com/codywd/WiFiz/issues"))
TypeError: cannot concatenate 'str' and 'bool' objects
START /usr/bin/chromium "https://github.com/codywd/WiFiz/issues"
[7250:7250:0201/231848:ERROR:chrome_browser_main_extra_parts_gtk.cc(50)] Startup refusing to run as root.

我了解为什么会发生此错误,但我不知道如何执行此代码。

提前谢谢各位!

4

1 回答 1

0

看起来这里给出的答案就是您要寻找的答案: Run child processes as different user from a long running process

但是,由于我找不到直接访问在 webbrowser.open 内启动的子进程的方法(请参阅http://docs.python.org/2/library/webbrowser.html上的文档),您有两个选择:

  • 从您的代码中删除 webbrowser 模块,通过子进程调用打开浏览器。在此您可以设置 UID 和 GID 位。
  • 在网络浏览器周围设置一个包装器(在下面展开)

1)创建一个新的python脚本,从那里正常启动webbrowser.open。

2) 在你的主程序中创建一个子进程,使用正确的 UID + GID,这将调用你在 1) 中描述的包装脚本。

于 2014-02-02T07:33:52.093 回答