Commit 904ac85c authored by Ralf Schmitt's avatar Ralf Schmitt

backdoor.py: simplify code. also close connection on SystemExit.

parent dc3493ed
......@@ -43,7 +43,7 @@ Miscellaneous:
- Fixed ``webproxy.py`` example to be runnable under external WSGI server.
- Fixed bogus failure in ``test__exc_info.py``.
- Added new test to check PEP8 conformance: ``xtest_pep8.py``
- Made :class:`BackdoorServer` close the connection on SystemExit and simplified the code
Version 0.13.0
--------------
......
......@@ -47,32 +47,9 @@ class SocketConsole(Greenlet):
def __init__(self, locals, conn):
Greenlet.__init__(self)
self.locals = locals
# mangle the socket
desc = self.desc = _fileobject(conn)
readline = desc.readline
self.old = {}
self.fixups = {
'softspace': 0,
'isatty': lambda: True,
'flush': lambda: None,
'readline': lambda *a: readline(*a).replace('\r\n', '\n'),
}
for key, value in self.fixups.iteritems():
if hasattr(desc, key):
self.old[key] = getattr(desc, key)
setattr(desc, key, value)
self.desc = _fileobject(conn)
def finalize(self):
# restore the state of the socket
for key in self.fixups:
try:
value = self.old[key]
except KeyError:
delattr(self.desc, key)
else:
setattr(self.desc, key, value)
self.fixups.clear()
self.old.clear()
self.desc = None
def switch(self, *args, **kw):
......@@ -89,7 +66,7 @@ class SocketConsole(Greenlet):
console = InteractiveConsole(self.locals)
console.interact()
except SystemExit: # raised by quit()
pass
sys.exc_clear()
finally:
self.switch_out()
self.finalize()
......@@ -111,6 +88,14 @@ class _fileobject(socket._fileobject):
def write(self, data):
self._sock.sendall(data)
def isatty(self):
return True
def flush(self):
pass
def readline(self, *a):
return socket._fileobject.readline(self, *a).replace("\r\n", "\n")
if __name__ == '__main__':
if not sys.argv[1:]:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment