python win32库与subprocess_Python subprocess.Popen作为Windows上的不同用户

我最终扩展了子流程:#!/usr/bin/env python

# -*- coding: utf-8 -*-

import subprocess

import win32con

import win32process

import win32security

from subprocess import *

__all__ = ["Popen","PIPE", "STDOUT", "call", "check_call",

"CalledProcessError", "CREATE_NEW_CONSOLE", "LoginSTARTUPINFO",

"STARTUPINFO"]

class LoginSTARTUPINFO(object):

"""

Special STARTUPINFO instance that carries login credentials. When a

LoginSTARTUPINFO instance is used with Popen, the process will be executed

with the credentials used to instantiate the class.

If an existing vanilla STARTUPINFO instance needs to be converted, it

can be supplied as the last parameter when instantiating LoginSTARTUPINFO.

The LoginSTARTUPINFO cannot be used with the regular subprocess module.

>>> import subprocesswin32 as subprocess

>>> sysuser = LoginSTARTUPINFO("username", "pswd123", "machine")

>>> stdout, stderr = subprocess.Popen("cmd.exe", stdout=subprocess.PIPE,

... startupinfo=sysuser).communicate()

"""

def __init__(self, username, domain, password, startupinfo=None):

m_startupinfo = win32process.STARTUPINFO()

# Creates an actual win32 STARTUPINFO class using the attributes

# of whatever STARTUPINFO-like object we are passed.

for attr in dir(startupinfo):

if not(attr.startswith("_") or attr not in dir(m_startupinfo)):

setattr(m_startupinfo, attr, getattr(startupinfo, attr))

# Login credentials

self.credentials = (username, domain, password)

# Proper win32 STARTUPINFO representation for CreateProcess

self.win32startupinfo = m_startupinfo

def CreateProcess(*args):

startupinfo = args[-1]

# If we are passed a LoginSTARTUPINFO, that means we need to use

# CreateProcessAsUser instead of the CreateProcess in subprocess

if isinstance(startupinfo, LoginSTARTUPINFO):

# Gets the actual win32 STARTUPINFO object from LoginSTARTUPINFO

win32startupinfo = startupinfo.win32startupinfo

mkprocargs = args[:-1] + (win32startupinfo,)

login, domain, password = startupinfo.credentials

# Get a user handle from the credentials

userhandle = win32security.LogonUser(login, domain, password,

win32con.LOGON32_LOGON_INTERACTIVE,

win32con.LOGON32_PROVIDER_DEFAULT)

try:

# Return the pipes from CreateProcessAsUser

return win32process.CreateProcessAsUser(userhandle, *mkprocargs)

finally:

# Close the userhandle before throwing whatever error arises

userhandle.Close()

return win32process.CreateProcess(*args)

# Overrides the CreateProcess module of subprocess with ours. CreateProcess

# will automatically act like the original CreateProcess when it is not passed

# a LoginSTARTUPINFO object.

STARTUPINFO = subprocess.STARTUPINFO = win32process.STARTUPINFO

subprocess._subprocess.CreateProcess = CreateProcess

以下代码以用户身份启动cmd.exe:>>> import subprocesswin32 as subprocess

>>> sysuser = LoginSTARTUPINFO("username", "pswd123", "machine")

>>> stdout, stderr = subprocess.Popen("cmd.exe", stdout=subprocess.PIPE,

... startupinfo=sysuser).communicate()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值