是否可以将Python脚本中的文本作为可执行命令输出到终端?

是否可以将Python脚本中的文本作为可执行命令输出到终端?,python,terminal,output,Python,Terminal,Output,具体地说,我需要一个Python脚本,它接受来自用户的字符串,并将该字符串解释为终端中的命令。换句话说,我的脚本应该可以按如下方式使用: python testScript.py "command -arg1 -arg2 -arg3" command -arg1 -arg2 -arg3 输出应如下所示: python testScript.py "command -arg1 -arg2 -arg3" command -arg1 -arg2 -arg3 它使用3个参数执行命令:arg1、a

具体地说,我需要一个Python脚本,它接受来自用户的字符串,并将该字符串解释为终端中的命令。换句话说,我的脚本应该可以按如下方式使用:

python testScript.py "command -arg1 -arg2 -arg3"
command -arg1 -arg2 -arg3
输出应如下所示:

python testScript.py "command -arg1 -arg2 -arg3"
command -arg1 -arg2 -arg3
它使用3个参数执行命令:arg1、arg2和arg3

i、 e

输出当前目录的权限

同样地

python testScript.py "/testarea ls -lah"
将输出目录“/testarea”的权限


任何建议或模块?

运行任意用户输入通常被认为是一个坏主意,但如果您真的想这样做:

#testScript.py
import sys, os

if __name__ == "__main__":
    os.system(" ".join(sys.argv[1:]))

运行任意用户输入通常被认为是一个坏主意©但如果您真的想这样做:

#testScript.py
import sys, os

if __name__ == "__main__":
    os.system(" ".join(sys.argv[1:]))

最可靠的方法是使用
子流程
模块。看看所有可能的选择


最可靠的方法是使用
子流程
模块。看看所有可能的选择

当然可以

最基本的方法是使用操作系统:

import os, sys

os.system(sys.argv[1])
如果您想更好地控制通话,请查看 但子流程模块。有了这个模块,你也可以做同样的事情 如上所述,但要做更多的事情,比如捕获 并在程序中使用它

当然

最基本的方法是使用操作系统:

import os, sys

os.system(sys.argv[1])
如果您想更好地控制通话,请查看 但子流程模块。有了这个模块,你也可以做同样的事情 如上所述,但要做更多的事情,比如捕获
这是我想到的最好的答案。我比那些说要使用
子流程
模块的人投了更高的票,或者有一个好的替代方案

import subprocess, threading

class Command(object):
    def __init__(self, cmd):
        self.cmd = cmd
        self.process = None

    def run(self, timeout):
        def target():
            print 'Thread started'
            self.process = subprocess.Popen(self.cmd, shell=True)
            self.process.communicate()
            print 'Thread finished'

        thread = threading.Thread(target=target)
        thread.start()

        thread.join(timeout)
        if thread.is_alive():
            print 'Terminating process'
            self.process.terminate()
            thread.join()
        print self.process.returncode

#This will run one command for 5 seconds:
command = Command("ping www.google.com")
command.run(timeout=5)
这将运行
ping www.google.com
命令5秒,然后超时。创建命令时,可以向列表中添加任意数量的参数,参数之间用空格分隔

这是命令ls-lah的一个示例:

command = Command("ls -lah")
command.run(timeout=5)
以及一次运行多个命令的示例:

command = Command("echo 'Process started'; sleep 2; echo 'Process finished'")
command.run(timeout=5)

简单而健壮,正是我喜欢的

这是我想到的最好的答案。我比那些说要使用
子流程
模块的人投了更高的票,或者有一个好的替代方案

import subprocess, threading

class Command(object):
    def __init__(self, cmd):
        self.cmd = cmd
        self.process = None

    def run(self, timeout):
        def target():
            print 'Thread started'
            self.process = subprocess.Popen(self.cmd, shell=True)
            self.process.communicate()
            print 'Thread finished'

        thread = threading.Thread(target=target)
        thread.start()

        thread.join(timeout)
        if thread.is_alive():
            print 'Terminating process'
            self.process.terminate()
            thread.join()
        print self.process.returncode

#This will run one command for 5 seconds:
command = Command("ping www.google.com")
command.run(timeout=5)
这将运行
ping www.google.com
命令5秒,然后超时。创建命令时,可以向列表中添加任意数量的参数,参数之间用空格分隔

这是命令ls-lah的一个示例:

command = Command("ls -lah")
command.run(timeout=5)
以及一次运行多个命令的示例:

command = Command("echo 'Process started'; sleep 2; echo 'Process finished'")
command.run(timeout=5)

简单而健壮,正是我喜欢的

在我看到这些评论之前,这就是我使用的解决方案(我使用子进程以及线程来限制每个进程可以执行的时间)。我也将发布我的最终答案,以便您可以看到此问题的最佳解决方案。在我看到这些评论之前,这是我使用的解决方案(我使用子流程以及线程来限制每个流程可以执行的时间)。我也将发布我的最终答案,以便您可以看到这个问题的最佳解决方案。子流程就是它所在的位置。我用它和线程来限制每个进程可以使用的时间。我发布了我的答案,但我真的很喜欢你的简单!我只是需要一个更健壮的解决方案,并且需要能够在需要时快速终止流程。子流程就是它所处的位置。我用它和线程来限制每个进程可以使用的时间。我发布了我的答案,但我真的很喜欢你的简单!我只需要一个更健壮的解决方案,需要能够在需要时快速终止流程。我同意,但我正在创建的应用程序是一个测试套件,我希望用户(我们的测试人员)在其中能够输入任意数量的参数并将其存储以供随时使用,而无需记住终端中200个不同终端命令的所有参数。我同意,但我正在创建的应用程序是一个测试套件,我希望用户(我们的测试人员)在其中使用能够输入任意数量的参数并将其存储以供随时使用,而无需记住终端中200个不同终端命令的所有参数。