我正在尝试构建一个应用程序,该应用程序使用wx模块作为图形部分。我的操作系统是windows 10,python版本是2.7。
此应用程序应检查是否有人远程连接到计算机,如果是,则更改按钮的颜色。要检查是否有人连接到计算机,我将解析qwinsta调用的输出。
我必须指定如下:要运行这个应用程序,我使用pythonw。
代码块如下:
def isLocked():
process =subprocess.Popen('qwinsta',stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
output,error = process.communicate()
print output
output2 = output[0:len(output)]
output2= output2.split('\n')问题是,我所做的子进程调用返回一个错误:
“‘qwinsta”不被识别为内部或外部命令、\r\n可操作的程序或批处理文件。\r\n“”
我想我找到了问题,但我不知道如何解决:
如果我用python调用它,这个子进程调用可以正常工作。如果我使用pythonw,它就失败了。在我看来,pythonw使用syswow64/cmd.exe,python使用system32 32/cmd.exe。
我检查了os.environ变量在python和pythonw上,并且COMSPEC变量是相同的。
发布于 2018-06-20 15:37:34
所以我找到了一个解决问题的方法。我仍然认为问题在于pythonw使用windows/syswow64/cmd.exe。此cmd似乎无法执行在Syste32文件夹中找到的qwinsta.exe可执行文件。
*但是这个qwista.exe也位于windows/WinSxS文件夹中,syswow64/cmd.exe可以访问该文件夹并能够运行该可执行文件。
所做的工作是,我给子进程打开了通向windows/WinSxS/amd64_microsoft-windows-t..commandlinetoolsmqq_31bf3856ad364e35...的路径qwinsta.exe
process =subprocess.Popen('',stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=False,executable='C:\Windows\WinSxS\amd64...\qwinsta.exe')发布于 2020-07-14 17:59:40
是的,解决办法很好,以下是一些更完整的代码,以防远程PC需要状态。
import subprocess
#This could be also the ip
remote_desktop_name = MY_PC_NAME
#Searh the path where qwinsta is located for 64 bits cmd
#It may start as C:\Windows\WinSxS\amd64_microsoft-windows-t ...
qwinsta_path = r'THE_PATH_WHERE_QWINSTA_IS_FOR_64BITS_CMD'
process = subprocess.Popen(['qwinsta', remote_desktop_name],
env = os.environ,
shell=True,
cwd = qwinsta_path,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
#Wait for the remote connection to respond
process.wait()
#Get error and output
out, err = process.communicate()
print(out)
print(err)https://stackoverflow.com/questions/50929915
复制相似问题