3
# Copyright (c) 2009-2010 Twisted Matrix Laboratories.
4
# See LICENSE for details.
6
from twisted.internet import protocol
7
from twisted.internet import reactor
10
class MyPP(protocol.ProcessProtocol):
11
def __init__(self, verses):
14
def connectionMade(self):
15
print "connectionMade!"
16
for i in range(self.verses):
17
self.transport.write("Aleph-null bottles of beer on the wall,\n" +
18
"Aleph-null bottles of beer,\n" +
19
"Take one down and pass it around,\n" +
20
"Aleph-null bottles of beer on the wall.\n")
21
self.transport.closeStdin() # tell them we're done
22
def outReceived(self, data):
23
print "outReceived! with %d bytes!" % len(data)
24
self.data = self.data + data
25
def errReceived(self, data):
26
print "errReceived! with %d bytes!" % len(data)
27
def inConnectionLost(self):
28
print "inConnectionLost! stdin is closed! (we probably did it)"
29
def outConnectionLost(self):
30
print "outConnectionLost! The child closed their stdout!"
31
# now is the time to examine what they wrote
32
#print "I saw them write:", self.data
33
(dummy, lines, words, chars, file) = re.split(r'\s+', self.data)
34
print "I saw %s lines" % lines
35
def errConnectionLost(self):
36
print "errConnectionLost! The child closed their stderr."
37
def processExited(self, reason):
38
print "processExited, status %d" % (reason.value.exitCode,)
39
def processEnded(self, reason):
40
print "processEnded, status %d" % (reason.value.exitCode,)
45
reactor.spawnProcess(pp, "wc", ["wc"], {})