remote control of a cmd.Cmd() loop (help requested)

MRAB python at mrabarnett.plus.com
Fri May 22 21:41:29 EDT 2015


On 2015-05-23 01:27, Benjamin Risher wrote:
> First, the snippet provided is a contrived example of a much larger program.
>
> http://pastebin.com/xRqBE5BY
>
> (written in python3 with access to 3.4 if necessary)
>
> The goal:  To connect to a listening socket and send commands to the cmd.Cmd() loop running, then show the output to both stdout and the remote connection.
>
> Right now, I have a solution that will work as long as everything that needs to get sent to stdout and the socket is return'd from each function instead of printed.
>
> Because this is already a larger project, I would prefer not to have to go back through and refactor everything to facilitate the remote control (the actual program involves threading and locks etc etc).
>
> I feel like there should be a fairly simple solution dealing with duplicating the file descriptor or something similar.  I've messed around trying to find something like that, but without success.
>
> I've also looked into the contextlib.redirect_stdout, but it expects a file type object.  Also, using socket.makefile() results in the returned object not having a fileno() method, so I can't use select on it.
>
> Basically, I'm hoping someone here knows some file descriptor-fu or some other cleaner solution.
>
> Any help would be appreciated!
>
>
> remote display ------------- | ------------ menu display
>
> $>nc localhost 12345         | s function called.
> s                            | h function called.
> s function called.           | Traceback ...
> h                            | 44: 'NoneType' object has no attribute 'strip'
>
> $>
>
You could replace the current sys.stdout object with your own object 
that copies to the original sys.stdout object and the socket.



More information about the Python-list mailing list