twisted使用进程

aab*_*rga 3 process twisted

我正在学习使用twisted(最新的12.3.0版本),作为一种为移动应用程序进行简单服务器端处理的方法.

我的第一个主要是在日志文件上运行'tail'命令,并将后处理的找到的行传递给移动应用程序.那应该很容易......

现在在TwistedMatrix网站上的文档中有一个"使用进程"页面,我在其中获得以下代码:


from twisted.internet import protocol, utils, reactor
from twisted.python import failure
from cStringIO import StringIO

class CommandRunner(protocol.Protocol):

    #command = "ls /"
    command = "tail -n 100 /var/log/system.log"

    def connectionMade(self):
        output = utils.getProcessOutput(self.command)
        output.addCallbacks(self.writeResponse, self.noResponse)

    def writeResponse(self, resp):
        self.transport.write(resp)
        self.transport.loseConnection()

    def noResponse(self, err):

        print err
        self.transport.write("Houston, we have an error!\n")
        self.transport.loseConnection()


if __name__ == '__main__':
    f = protocol.Factory()
    f.protocol = CommandRunner
    reactor.listenTCP(10999, f)
    reactor.run()
Run Code Online (Sandbox Code Playgroud)

它与"简单易行"下的已发布代码片段的99.9%完全相同.唯一的变化是扭曲应该执行的shell命令(在我的Mac上我似乎没有fortune命令).

当我尝试使用telnet从第二个终端连接10999端口时启动示例代码后,我收到此错误:

[失败实例:回溯(没有帧失败)::得到了stderr:'在execvpe tail -n 100 /var/log/system.log [\'tail -n 100 /var/log/system.log \']中环境ID 4315532016 \n:Traceback(最近一次调用最后一次):\n文件"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Twisted-12.3.0-py2.7 -macosx-10.6-intel.egg/twisted/internet/process.py",第420行,在_fork \n可执行文件,args,环境中)\n文件"/Library/Frameworks/Python.framework/Versions/2.7/lib/ python2.7/site-packages/Twisted-12.3.0-py2.7-macosx-10.6-intel.egg/twisted/internet/process.py",第466行,_execChild \n os.execvpe(可执行文件,args,环境)\n文件"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py",第353行,在execvpe \n _execvpe(文件,args,env)\n文件"/库/框架/ Python.framework/Versions/2.7/lib/python2.7/os.py",第368行,在_execvpe \n func(文件,*argrest)\n \n \n错误:[错误2]没有这样的文件或目录\ N']

我没有看到任何明显的原因,为什么代码应该提交[Errno 2]没有这样的文件或目录\n']错误..

蒂亚

Jea*_*one 5

你想要运行的程序是"尾巴".您想要传递几个参数:" - n","100"和"/var/log/system.log".

相反,你的代码所做的是运行程序"tail -n 100 /var/log/system.log",这可能在你的系统上不存在(我不希望它).

正确使用getProcessOutput是将程序与参数列表分开传递:

getProcessOutput("tail", ["-n", "100", "/var/log/system.log"])
Run Code Online (Sandbox Code Playgroud)