如何确定运行Python脚本的用户和组? [英] How to determine what user and group a Python script is running as?

查看:124
本文介绍了如何确定运行Python脚本的用户和组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CGI脚本,该脚本在Web服务器的错误日志中的堆栈跟踪中得到了"IOError: [Errno 13] Permission denied"错误.

作为调试此问题的一部分,我想向脚本中添加一些代码,以将运行脚本的用户和(特别是)运行组打印到错误日志(可能是STDERR)中. /p>

我知道我可以将值打印到sys.stderr,但是我如何确定脚本运行的用户和组呢?

(我对组特别感兴趣,因此$USER环境变量将无济于事; CGI脚本设置了setgid位,因此应作为组列表"而不是Web服务器的"www"运行-data"-但我需要代码来查看这是否确实在发生.)

解决方案

您可以使用以下代码:

import os
print(os.getegid())

I have a CGI script that is getting an "IOError: [Errno 13] Permission denied" error in the stack trace in the web server's error log.

As part of debugging this problem, I'd like to add a little bit of code to the script to print the user and (especially) group that the script is running as, into the error log (presumably STDERR).

I know I can just print the values to sys.stderr, but how do I figure out what user and group the script is running as?

(I'm particularly interested in the group, so the $USER environment variable won't help; the CGI script has the setgid bit set so it should be running as group "list" instead of the web server's "www-data" - but I need code to see if that's actually happening.)

解决方案

You can use the following piece of code:

import os
print(os.getegid())

这篇关于如何确定运行Python脚本的用户和组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆