| 1 |
# netfraggletelnetd.tac |
| 2 |
# Copyright (C) 2004 netfrag.org |
| 3 |
# |
| 4 |
# from: |
| 5 |
# http://twistedmatrix.com/pipermail/twisted-python/2004-August/008335.html |
| 6 |
# |
| 7 |
# This library is free software; you can redistribute it and/or |
| 8 |
# modify it under the terms of version 2.1 of the GNU Lesser General Public |
| 9 |
# License as published by the Free Software Foundation. |
| 10 |
# |
| 11 |
# This library is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
# Lesser General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU Lesser General Public |
| 17 |
# License along with this library; if not, write to the Free Software |
| 18 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
|
| 20 |
|
| 21 |
# see also: |
| 22 |
# http://www.twistedmatrix.com/documents/current/examples/pbecho.py |
| 23 |
# http://www.twistedmatrix.com/documents/current/examples/cursesclient.py |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
# start with "twistd -y <this_file.tac>" |
| 28 |
|
| 29 |
from twisted.internet import reactor, app, tcp |
| 30 |
#from twisted.protocols import telnet |
| 31 |
from twisted.manhole import telnet |
| 32 |
from twisted.application import service,internet |
| 33 |
import sys |
| 34 |
|
| 35 |
class MyShell(telnet.Shell): |
| 36 |
def doCommand(self, cmd): |
| 37 |
|
| 38 |
# TODO -- refactor this, Reality.author.Author, and the manhole shell |
| 39 |
#to use common functionality (perhaps a twisted.python.code module?) |
| 40 |
fn = '$telnet$' |
| 41 |
result = None |
| 42 |
try: |
| 43 |
out = sys.stdout |
| 44 |
sys.stdout = self |
| 45 |
try: |
| 46 |
if cmd == 'exit': self.transport.loseConnection() |
| 47 |
#code = compile(cmd,fn,'eval') |
| 48 |
#result = eval(code, self.factory.namespace) |
| 49 |
result = "hello" |
| 50 |
except: |
| 51 |
try: |
| 52 |
#code = compile(cmd, fn, 'exec') |
| 53 |
#exec code in self.factory.namespace |
| 54 |
result = "world" |
| 55 |
except SyntaxError, e: |
| 56 |
if not self.lineBuffer and str(e)[:14] == "unexpected EOF": |
| 57 |
self.lineBuffer.append(cmd) |
| 58 |
self.transport.write("... ") |
| 59 |
return |
| 60 |
else: |
| 61 |
failure.Failure().printTraceback(file=self) |
| 62 |
log.deferr() |
| 63 |
self.write('\r\n>>> ') |
| 64 |
return |
| 65 |
except: |
| 66 |
io = StringIO() |
| 67 |
failure.Failure().printTraceback(file=self) |
| 68 |
log.deferr() |
| 69 |
self.write('\r\n>>> ') |
| 70 |
return |
| 71 |
finally: |
| 72 |
sys.stdout = out |
| 73 |
|
| 74 |
self.factory.namespace['_'] = result |
| 75 |
if result is not None: |
| 76 |
self.transport.write(repr(result)) |
| 77 |
self.transport.write('\r\n') |
| 78 |
self.transport.write(">>> ") |
| 79 |
|
| 80 |
|
| 81 |
class MyShellFactory(telnet.ShellFactory): |
| 82 |
protocol = MyShell |
| 83 |
|
| 84 |
application = service.Application('telnet') |
| 85 |
ts = MyShellFactory() |
| 86 |
reactor.listenTCP(4040, ts) |