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

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

问题描述

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

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.

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

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).

我知道我可以将值打印到 sys.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?

(我对组特别感兴趣,所以 $USER 环境变量无济于事;CGI 脚本设置了 setgid 位,因此它应该作为组list"运行网络服务器的www-data" - 但我需要代码来看看这是否真的发生了.)

(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.)

推荐答案

您可以使用以下代码:

import os
print(os.getegid())

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

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