Python无法从特权降级的根脚本执行Firefox Web浏览器
我无法通过一个用sudo权限运行的Python脚本来启动Firefox,然后再把权限降到普通用户。如果我写了
$ sudo python >>> import os >>> import pwd, grp >>> uid = pwd.getpwnam('norby')[2] >>> gid = grp.getgrnam('norby')[2] >>> os.setegid(gid) >>> os.seteuid(uid) >>> import webbrowser >>> webbrowser.get('firefox').open('www.google.it') True >>> # It returns true but doesn't work >>> from subprocess import Popen,PIPE >>> p = Popen('firefox www.google.it', shell=True,stdout=PIPE,stderr=PIPE) >>> # Doesn't execute the command >>> You shouldn't really run Iceweasel through sudo WITHOUT the -H option. Continuing as if you used the -H option. No protocol specified Error: cannot open display: :0
我觉得这不是Python的问题,而是Firefox、Iceweasel或者Debian的配置问题。可能Firefox只读取用户的UID,而不是EUID,所以因为UID是0,它就不执行这个进程。你怎么看?
1 个回答
1
这可能是你的环境设置。即使你改变了权限,像$HOME这样的环境变量仍然会指向根用户的目录,而这个目录是无法访问的。你可以尝试在启动浏览器之前,通过修改os.environ
来改变这些变量。还有其他一些变量也值得检查一下。