【问题标题】:Running a portion of code as NON-root?以非根用户身份运行部分代码?
【发布时间】:2014-02-02 06:24:36
【问题描述】:

我的程序以 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.

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

提前谢谢各位!

【问题讨论】:

    标签: python-2.7 root sudo python-webbrowser


    【解决方案1】:

    看起来这里给出的答案就是您想要的: 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) 中描述的包装脚本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 2010-11-13
      • 2015-01-04
      • 1970-01-01
      • 2023-03-25
      • 2017-07-08
      • 2017-06-22
      相关资源
      最近更新 更多