Is there a way to interactively program a Python curses application?

前端 未结 3 1456
盖世英雄少女心
盖世英雄少女心 2020-12-30 05:47

Is there a way to create a second terminal so that all calls to curses functions operate on that, rather than in the existing terminal? I work much faster when

3条回答
  •  温柔的废话
    2020-12-30 06:32

    You could use code.InteractiveConsole and SocketServer to attach a python interactive shell to a socket and do your development through that. A simple example looks like:

    import sys
    import SocketServer
    from code import InteractiveConsole
    
    class InteractiveServer(SocketServer.BaseRequestHandler):
       def handle(self):
            file = self.request.makefile()
            shell = Shell(file)
            try:
               shell.interact()
            except SystemExit:
               pass
    
    
    class Shell(InteractiveConsole):
        def __init__(self, file):
            self.file = sys.stdout = file
            InteractiveConsole.__init__(self)
            return
    
        def write(self, data):
           self.file.write(data)
           self.file.flush()
    
        def raw_input(self, prompt=""):
           self.write(prompt)
           return self.file.readline()
    
    if __name__ == '__main__':
       HOST, PORT = "0.0.0.0", 9999
    
       server = SocketServer.TCPServer((HOST, PORT), InteractiveServer)
       server.serve_forever()
    

    Once you've got that up and running you can connect to port 9999 from another terminal and do your thing. You can see this working in this screenshot (PNG)

    The basics for using the InteractiveConsole were taken from this post. I modified it to work with the SocketServer for another project I was working on.

提交回复
热议问题