我的问题对于一些有经验的Python程序员来说可能非常容易(我不是其中之一)。通过instance.cmd调用带参数的函数()
我想用参数启动服务器。
目前我做:
def startServer(host):
host.cmd('python server.py &')
有没有办法将参数传递到server.py?
我的问题对于一些有经验的Python程序员来说可能非常容易(我不是其中之一)。通过instance.cmd调用带参数的函数()
我想用参数启动服务器。
目前我做:
def startServer(host):
host.cmd('python server.py &')
有没有办法将参数传递到server.py?
为了从命令行运行python run server.py
,我们可以使用subprocess
模块。文档可以在这里找到:https://docs.python.org/2/library/subprocess.html。
import subprocess
def startServer(host, argument):
subprocess.call(['python', 'server.py', argument])
当调用startServer
功能与一些参数(比如an_argument_to_server
),它会在命令行中运行python server.py an_argument_to_server
。我们不需要&
字符,因为subprocess
会自动在后台运行。 subprocess
模块也适用于python 3.x
我很确定OP使用的是ssh库......其中host是一个ssh实例,它的目标是一个远程shell +1,但是如果你要使用子进程,你应该像' [“cmd”,“arg1”,“arg2”]'你真的不需要'&',因为它可以自动并行运行,子进程 –
这些都是很好的补充。我会添加到答案。 – SNygard
你想使用哪些参数?另外,'host'将会是什么? –
什么是'主机'? –
主机是“Mininet”中的一个节点,它是一个虚拟网络......也许cmd是该类的一个参数。我认为它可能会在python – Michael