3

モバイルアプリの単純なサーバー側処理を行う方法として、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()

これは、「Doing it the EasyWay」で公開されているコードスニペットと99.9%同一です。唯一の変更は、twistedが実行する必要のあるシェルコマンドです(私のMacでは、fortuneコマンドがないようです)。

サンプルコードを起動した後、telnetを使用して2番目の端末から10999ポートに接続しようとすると、次のエラーが発生します。

[失敗インスタンス:トレースバック(フレームなしの失敗):: got stderr:'Upon execvpe tail -n 100 /var/log/system.log [\' tail -n 100 /var/log/system.log \'] in環境ID4315532016\ n:トレースバック(最後の最後の呼び出し):\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実行可能、引数、環境)\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(executable、a​​rgs、環境)\nファイル"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py"、353行目、execvpe \ n _execvpe(file、args、env)\nファイル"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py"、行368、_execvpe \ n func(file、* argrest)\ nOSError:[Errno 2 ]そのようなファイルやディレクトリはありません\n']

コードが[Errno2]そのようなファイルまたはディレクトリはありません\n']エラーでファイルする必要がある明確な理由はわかりません。

ティア

4

1 に答える 1

5

実行したいプログラムは「tail」です。「-n」、「100」、および「/var/log/system.log」といういくつかの引数を渡します。

代わりに、あなたのコードはプログラム "tail -n 100 /var/log/system.log" を実行しますが、これはおそらくあなたのシステムには存在しません (私はそうは思いません)。

の適切な使用法はgetProcessOutput、引数リストとは別にプログラムを渡すことです。

getProcessOutput("tail", ["-n", "100", "/var/log/system.log"])
于 2013-02-28T17:29:07.963 に答える