[英]Python subprocess python script
I want to create python console that can run not only regular commands but also python and other commands.我想创建不仅可以运行常规命令还可以运行 python 和其他命令的 python 控制台。 For example how can I run this script and provide input with subprocess?
例如,我如何运行此脚本并为子进程提供输入?
print("Welcome to simple program !")
data = input("Enter something: ")
print(data)
Try this:尝试这个:
import subprocess
print("Welcome to simple program !")
cmd= input("Enter something: ")
subprocess.run(cmd, shell=True)
Security Considerations Unlike some other popen functions, this implementation will never implicitly call a > system shell.
安全考虑与其他一些 popen 函数不同,此实现永远不会隐式调用 > 系统外壳。 This means that all characters, including shell metacharacters, can safely be passed to child processes.
这意味着所有字符,包括 shell 元字符,都可以安全地传递给子进程。 If the shell is invoked explicitly, via shell=True, it is the application's responsibility to ensure that all whitespace and metacharacters are quoted appropriately to avoid shell injection vulnerabilities.
如果通过 shell=True 显式调用 shell,则应用程序有责任确保所有空格和元字符都被正确引用以避免 shell 注入漏洞。 When using shell=True, the shlex.quote() function can be used to properly escape whitespace and shell metacharacters in strings that are going to be used to construct shell commands.
使用 shell=True 时,shlex.quote() 函数可用于正确转义将用于构造 shell 命令的字符串中的空格和 shell 元字符。 reference
参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.