[英]How can I stop a program's execution with python?
I use a program (on Windows), whose name I won't disclose, that can be opened from the command line without going through any authentication. 我使用了一个程序(在Windows上),但我不会透露其名称,该程序无需任何身份验证即可从命令行打开。 I'm trying to create some security to prevent others from accessing it this way.
我正在尝试创建一些安全性,以防止其他人以这种方式访问它。
I plan on replacing the built-in binary for this program with a batch file with a first line that points to my own authentication system (implemented in python, compiled to .exe with py2exe), and the second line to the command that opens the program. 我计划用一个批处理文件替换该程序的内置二进制文件,其中第一行指向我自己的身份验证系统(在python中实现,用py2exe编译为.exe),第二行打开该命令。程序。
My original plan was to have my script (auth.py) stop the batch file from executing the second line if authentication failed with something like this: 我最初的计划是让我的脚本(auth.py)在验证失败的情况下阻止批处理文件执行第二行,如下所示:
if not authenticated:
print('Authentication failed')
sys.exit(1)
else:
print('Successfully authenticated!')
I had counted on sys.exit(1) to do this, and I didn't bother testing it out until I was done developing the script. 我指望sys.exit(1)可以做到这一点,直到完成脚本开发之后,我才开始进行测试。 Now I realize that sys.exit only exits the python process.
现在我意识到sys.exit仅退出python进程。
I either need a way to stop the batch process FROM the python script, or a way for the batch to detect the exit code from auth.py (1 if failed, 0 if passed) and execute the other program only if the exit code is 0 from there. 我或者需要一种方法来停止python脚本中的批处理过程,或者需要一种方法来使批处理从auth.py中检测退出代码(如果失败则为1,如果通过则为0),并且仅当退出代码为时才执行其他程序从那里0。
If anyone could give me any suggestions or help of any sort, I would really appreciate it. 如果有人可以给我任何建议或帮助,我将不胜感激。 Thanks!
谢谢!
Use subprocess
to call the program on successful authentication. 使用
subprocess
在成功认证后调用程序。 So your python script would launch the program, not a batch file. 因此,您的python脚本将启动程序,而不是批处理文件。
if not authenticated:
print('Authentication failed')
sys.exit(1)
else:
print('Successfully authenticated!')
proc = subprocess.Popen([program])
Please note, if the user has permission to start the program from within a python or batch script, nothing is stopping them from accessing it directly. 请注意,如果用户有权从python或批处理脚本中启动程序,则不会阻止他们直接访问该程序。 This will prevent no one from accessing the program;
这样可以防止任何人访问该程序; save maybe the extreme un-technical.
节省极端的技术性。
You could do something really complicated to try and find the parent PID of the Python process and kill that or you could just check the %ERRORLEVEL%
in your batch file. 您可以做一些非常复杂的事情来尝试找到Python进程的父PID并杀死它,或者可以只检查批处理文件中的
%ERRORLEVEL%
。 Something like: 就像是:
python auth.py
if %ERRORLEVEL% neq 0 exit /B 1
I found these two methods hope these might help 我发现这两种方法希望对您有所帮助
http://metazin.wordpress.com/2008/08/09/how-to-kill-a-process-in-windows-using-python/ http://metazin.wordpress.com/2008/08/09/how-to-kill-a-process-in-windows-using-python/
http://code.activestate.com/recipes/347462-terminating-a-subprocess-on-windows/ http://code.activestate.com/recipes/347462-terminating-a-subprocess-on-windows/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.