Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91085

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

Date 2015-05-23 02:41 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: remote control of a cmd.Cmd() loop (help requested)
References <d19c254b-7983-4862-9ab1-9ac45df841c1@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.247.1432345304.17265.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

remote control of a cmd.Cmd() loop (help requested) Benjamin Risher <brisher777@gmail.com> - 2015-05-22 17:27 -0700
  Re: remote control of a cmd.Cmd() loop (help requested) MRAB <python@mrabarnett.plus.com> - 2015-05-23 02:41 +0100

csiph-web