Commits

Ralf Schmitt committed a6da624

add optional banner argument to BackdoorServer

  • Participants
  • Parent commits 691f2bc

Comments (0)

Files changed (2)

gevent/backdoor.py

 
 class SocketConsole(Greenlet):
 
-    def __init__(self, locals, conn):
+    def __init__(self, locals, conn, banner=None):
         Greenlet.__init__(self)
         self.locals = locals
         self.desc = _fileobject(conn)
+        self.banner = banner
 
     def finalize(self):
         self.desc = None
         try:
             try:
                 console = InteractiveConsole(self.locals)
-                console.interact()
+                console.interact(banner=self.banner)
             except SystemExit:  # raised by quit()
                 sys.exc_clear()
         finally:
 
 class BackdoorServer(StreamServer):
 
-    def __init__(self, listener, locals=None, **server_args):
+    def __init__(self, listener, locals=None, banner=None, **server_args):
         StreamServer.__init__(self, listener, spawn=None, **server_args)
         self.locals = locals
+        self.banner = banner
         # QQQ passing pool instance as 'spawn' is not possible; should it be fixed?
 
     def handle(self, conn, address):
-        SocketConsole.spawn(self.locals, conn)
+        SocketConsole.spawn(self.locals, conn, banner=self.banner)
 
 
 class _fileobject(socket._fileobject):

greentest/test__backdoor.py

             server.stop()
 
 
+    def test_banner(self):
+        banner = "Welcome stranger!"
+        server = backdoor.BackdoorServer(('127.0.0.1', 0), banner=banner)
+        server.start()
+        try:
+            conn = socket.create_connection(('127.0.0.1', server.server_port))
+            response = read_until(conn, '>>> ')
+            self.assertEqual(response[:len(banner)], banner)
+        finally:
+            server.stop()
+
+
 if __name__ == '__main__':
     greentest.main()