1 # Copyright (c) Twisted Matrix Laboratories
2 # See LICENSE for details
5 A PTY server that spawns a shell upon connection.
7 Run this example by typing in:
10 Telnet to the server once you start it by typing in:
11 > telnet localhost 5823
14 from twisted.internet import reactor, protocol
16 class FakeTelnet(protocol.Protocol):
17 commandToRun = ['/bin/sh'] # could have args too
19 def connectionMade(self):
20 print 'connection made'
21 self.propro = ProcessProtocol(self)
22 reactor.spawnProcess(self.propro, self.commandToRun[0], self.commandToRun, {},
23 self.dirToRunIn, usePTY=1)
24 def dataReceived(self, data):
25 self.propro.transport.write(data)
26 def conectionLost(self):
27 print 'connection lost'
28 self.propro.tranport.loseConnection()
30 class ProcessProtocol(protocol.ProcessProtocol):
32 def __init__(self, pr):
35 def outReceived(self, data):
36 self.pr.transport.write(data)
38 def processEnded(self, reason):
39 print 'protocol conection lost'
40 self.pr.transport.loseConnection()
42 f = protocol.Factory()
43 f.protocol = FakeTelnet
44 reactor.listenTCP(5823, f)