Path: csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python3': 0.05; '------------': 0.07; 'method,': 0.07; 'socket': 0.07; 'subject:help': 0.07; '(written': 0.09; 'benjamin': 0.09; 'called.': 0.09; 'descriptor': 0.09; 'snippet': 0.09; 'stdout': 0.09; 'sys.stdout': 0.09; 'output': 0.15; 'appreciated!': 0.16; 'cleaner': 0.16; 'contrived': 0.16; 'duplicating': 0.16; 'expects': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'localhost': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'socket.': 0.16; 'threading': 0.16; 'wrote:': 0.16; 'looked': 0.16; 'attribute': 0.18; 'project,': 0.18; 'solution.': 0.18; 'first,': 0.20; 'trying': 0.22; '(the': 0.22; 'fairly': 0.22; 'object.': 0.22; 'select': 0.23; "i've": 0.24; 'header:In-Reply-To:1': 0.24; 'example': 0.25; 'header:User- Agent:1': 0.26; 'actual': 0.29; "i'm": 0.29; 'etc).': 0.29; 'locks': 0.29; 'function': 0.30; "can't": 0.32; 'knows': 0.32; 'returned': 0.32; 'traceback': 0.33; 'file': 0.34; 'skip:- 10': 0.34; 'that,': 0.34; 'could': 0.35; 'to:addr:python-list': 0.35; 'replace': 0.35; 'something': 0.35; 'remote': 0.35; 'but': 0.36; 'there': 0.36; 'success.': 0.36; 'should': 0.37; 'display': 0.37; 'subject:: ': 0.37; 'instead': 0.38; 'someone': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'subject: (': 0.40; 'some': 0.40; 'your': 0.60; 'back': 0.61; 'simple': 0.61; 'show': 0.62; 'here': 0.66; 'results': 0.66; 'hoping': 0.73; 'connection.': 0.76; '3.4': 0.84; 'goal:': 0.84; 'messed': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=ZuhjKrLG c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=QrohdLjRRo4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=fGO4tVQLAAAA:8 a=95cauuK4M8NsP4SiBK4A:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Date: Sat, 23 May 2015 02:41:29 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: remote control of a cmd.Cmd() loop (help requested) References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1432345304 news.xs4all.nl 2932 [2001:888:2000:d::a6]:55790 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91085 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.