惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
IT之家
IT之家
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
博客园 - 【当耐特】
Recorded Future
Recorded Future
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
Last Week in AI
Last Week in AI
S
Schneier on Security
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
AWS News Blog
AWS News Blog
D
Docker
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
Spread Privacy
Spread Privacy
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
P
Palo Alto Networks Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
T
Tenable Blog
A
Arctic Wolf
C
Cisco Blogs
S
SegmentFault 最新的问题
T
The Exploit Database - CXSecurity.com
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Martin Fowler
Martin Fowler
G
GRAHAM CLULEY
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Latest news
Latest news
J
Java Code Geeks
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
L
LangChain Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
腾讯CDC
I
Intezer
Schneier on Security
Schneier on Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org

博客园 - 季石磊

jps 报process information unavailable解决办法 Jobtracker重启Job recovery过程分析 hadoop & hbase 上下线 Hbase性能优化 Poor performance running Hadoop on RHEL 6.2 or later when transparent hugepage compaction is enabled ldconfig几个需要注意的地方[转] [转]HBase性能优化方法总结 centos 查看版本 关于Hbase集群需要使用DNS域名解析的体会 修改Hadoop集群的备份数 [转载]ext4的noatime 指定HADOOP_HOME位置的三种方法 Windows下 Python 安装包的配置 django1.4测试环境 css样式,图片路径问题解决 CentOS 6安装JDK及系统配置 mapreduce程序运行避免手动删除output目录 HDFS配置设置 Hadoop运维操作 HDFS Permissions & Acls
Python用subprocess的Popen实现用户切换
季石磊 · 2013-01-17 · via 博客园 - 季石磊

当我们需要调用系统的命令的时候,最先考虑的os模块。用os.system()和os.popen()来进行操作,很多时候需要进行用户身份模拟或者用户切换,通过设置subprocess中的Popen的env变量可以实现相关功能。

创建一个可以执行文件,文件命名为:echoenv.py,该文件的作用是显示当前系统用户,文件内容如下:

import os
print(os.environ['USER'])

执行代码如下:

>>>import shlex, subprocess
>>>command_line = raw_input()
>>>python echoenv.py
>>>args = shlex.split(command_line)
>>>print args
>>>p = subprocess.Popen(args)

显示结果:
>>>root

>>>env={'USER':'abc'}
>>>p = subprocess.Popen(args,env=env)

显示结果:
>>>abc