Commits

Miki Tebeka committed 528f125

Comments and nicer main

  • Participants
  • Parent commits 1708798

Comments (0)

Files changed (1)

 #!/usr/bin/env python
 '''A "backdoor" shell for running servers (very much like Twisted manhole).
 
+Once installed, you can "telnet <host> <port>" and run Python commands on your
+server environment. This is very helpful in debugging servers.
+
 This one uses only modules found in the standard library.
 '''
 
 
     def handle(self):
         env = self.env.copy()
-        self.wfile.write("Welcome to ingress (type 'exit()' to exit)")
+        self.wfile.write("Welcome to ingress (type 'exit()' to exit)\n")
         while True:
             try:
                 self.wfile.write(">>> ")
     # Create a new handler class for this with it's own env
     class Handler(PyHandler):
         pass
-    Handler.env = env
+    Handler.env = env.copy()
 
     server = ThreadedServer(("localhost", port), Handler)
     server.serve_forever()
 
 DEFAULT_PORT = 9998
-def serve(env=None, port=DEFAULT_PORT):
-    env = env.copy() if env else {}
+def install(env=None, port=DEFAULT_PORT):
+    env = env or {}
     t = Thread(target=server_thread, args=(env, port))
     t.daemon = True
     t.start()
 
     return t
 
+def main(argv=None):
+    import sys
+    from argparse import ArgumentParser
+
+    argv = argv or sys.argv
+
+    parser = ArgumentParser(description="Run demo server.")
+    parser.add_argument("-p", "--port", help="port to listen",
+                        default=DEFAULT_PORT)
+    args = parser.parse_args(argv[1:])
+
+    t = install(port=args.port)
+    print("Serving on port {0}".format(args.port))
+    t.join()
+
+
 if __name__ == "__main__":
-    t = serve()
-    print("Serving on port {0}".format(DEFAULT_PORT))
-    t.join()
+    main()
+