モバイルアプリの単純なサーバー側処理を行う方法として、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、args、環境)\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']エラーでファイルする必要がある明確な理由はわかりません。
ティア