forked from srinathava/raspberry-pi-sleep-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessProtocolUtils.py
43 lines (33 loc) · 1.21 KB
/
ProcessProtocolUtils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from twisted.internet import protocol
import sys
import os
class TerminalEchoProcessProtocol(protocol.ProcessProtocol):
def __init__(self):
self.outdata = ''
self.errdata = ''
def outLineReceived(self, line):
print line
sys.stdout.flush()
def errLineReceived(self, line):
print>>sys.stderr, line
def outReceived(self, data):
self.outdata += data
lines = self.outdata.split('\n')
for line in lines[:-1]:
self.outLineReceived(line)
self.outdata = lines[-1]
def errReceived(self, data):
self.errdata += data
lines = self.errdata.split('\n')
for line in lines[:-1]:
self.errLineReceived(line)
self.errdata = lines[-1]
def dataReceived(self, data):
self.outReceived(data)
def spawnNonDaemonProcess(reactor, protocol, executable, args):
proc = reactor.spawnProcess(protocol, executable, args, os.environ)
reactor.addSystemEventTrigger('before', 'shutdown', lambda: proc.signalProcess('TERM'))
if __name__ == "__main__":
from twisted.internet import reactor
proc = reactor.spawnProcess(TerminalEchoProcessProtocol(), 'nc', ['nc', '-l', '1234'])
reactor.run()