Python无法从权限下降的root执行脚本执行firefox webbrowser

时间:2009-07-16 19:38:18

标签: python browser debian uid

我无法从一个将权限下放到普通用户的sudoed 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 个答案:

答案 0 :(得分:1)

这可能是您的环境。更改权限仍会使$ HOME等环境变量指向root用户的目录,该目录将无法访问。在启动浏览器之前更改os.environ可能值得尝试更改这些变量。可能还有其他值得检查的变量。