2013-02-28 21 views
3

我正在學習使用扭曲(最新的12.3.0版本),作爲爲移動應用程序做一些簡單的服務器端處理的一種方式。twisted使用進程

我的第一個分配基本上是在日誌文件上運行'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() 

是99.9%相同發佈到「簡單易行」的發佈代碼片段中。唯一的變化是應該執行扭曲的shell命令(在我的Mac上我似乎沒有命運的命運)。

啓動示例代碼,當我嘗試在10999端口上從通過telnet第二終端連接後,我得到這個錯誤:

[Failure instance: Traceback (failure with no frames): : got stderr: 'Upon execvpe tail -n 100 /var/log/system.log [\'tail -n 100 /var/log/system.log\'] in environment id 4315532016\n:Traceback (most recent call last):\n File "/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", line 420, in _fork\n executable, args, environment)\n File "/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", line 466, in _execChild\n os.execvpe(executable, args, environment)\n File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 353, in execvpe\n _execvpe(file, args, env)\n File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 368, in _execvpe\n func(file, *argrest)\nOSError: [Errno 2] No such file or directory\n']

我看不出有什麼明顯的原因代碼應與文件[錯誤2]沒有這樣的文件或目錄\ n']錯誤..

蒂亞

回答

5

要運行的程序是‘尾巴’。你想傳遞幾個參數:「-n」,「100」和「/var/log/system.log」。

相反,你的代碼所做的是運行程序「tail -n 100 /var/log/system.log」,這可能不存在於你的系統上(我不會期望它)。

正確使用getProcessOutput是從參數列表分別通過方案:

getProcessOutput("tail", ["-n", "100", "/var/log/system.log"]) 
+1

讓·保羅,謝謝! 在Mac(或任何其他地方)顯然沒有「tail -n 100 /var/log/system.log」... 更正調用getProcessOutput的方法可修復問題! 謝謝,阿爾多 – aaberga 2013-02-28 17:34:16

相關問題