如何将输出重定向到客户端

时间:2013-02-14 06:27:53

标签: python xml-rpc

我是Python中相对较新的程序员,我创建了这个XMLRPC Server函数,如下所示:

def shell(self, command, username):
    if username in loggedIn:
        return os.system(command)
    else:
        string = time.asctime() , " not logged in"
        string = "".join(string)
        return string

对于客户端,我写了

command = raw_input ("$ ")
if command == "exit":
    exit()
else:
    server.shell(command, username)

但是,当我在客户端程序中运行命令时,输出将在服务器窗口而不是客户端窗口中,如下所示:

#client side
$ ls

#server side
#some results
localhost - - [14/Feb/2013 14:26:25] "POST /RPC2 HTTP/1.0" 200 -

cd命令也被破坏(即使发出命令,我也无法更改到其他目录)。有没有办法这样做,如果有,怎么做?

1 个答案:

答案 0 :(得分:0)

os.system不会将输出返回到命令行,只返回返回码。如果要捕获stdout / stderr,则必须使用subprocess.Popen。然后,您还可以提供一个工作目录来执行命令(使用cwd参数)。据我所知,cd可能在shell中工作,但不会更改python实例的工作目录。因此,下一个调用将不会在您更改的目录中运行。使用带有cwd的Popen应该可以工作。

相关问题