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


Groups > comp.lang.python > #30942

Re: how to run shell command like "<<EOT .... EOT"

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ye.youqun@eisoo.com>
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; 'python,': 0.02; 'exception': 0.03; 'output': 0.04; '"""': 0.05; 'lines,': 0.05; 'emulate': 0.07; 'exit': 0.07; 'method,': 0.07; 'parameter': 0.07; 'python': 0.09; 'argument:': 0.09; 'blocking': 0.09; 'command.': 0.09; 'exception,': 0.09; 'path.': 0.09; 'res': 0.09; 'sep': 0.09; 'skip:< 20': 0.09; 'subject:command': 0.09; 'target,': 0.09; 'cc:addr:python-list': 0.10; 'sat,': 0.15; '"by': 0.16; '"use': 0.16; '(),': 0.16; 'binary,': 0.16; 'code?': 0.16; 'errors)': 0.16; 'expected,': 0.16; 'failed.': 0.16; 'fine.': 0.16; 'received:192.168.8': 0.16; 'subject:run': 0.16; 'subprocess': 0.16; 'wrote:': 0.17; 'shell': 0.18; '>>>': 0.18; 'input': 0.18; 'memory': 0.18; 'skip:p 30': 0.20; 'variable': 0.20; 'parameters': 0.20; 'all,': 0.21; 'pipe': 0.22; 'cc:2**0': 0.23; 'work.': 0.23; 'this:': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'device': 0.24; 'command': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'am,': 0.27; 'possibility': 0.27; 'wonder': 0.27; 'replace': 0.27; "doesn't": 0.28; 'run': 0.28; '>>>>': 0.29; 'buffers': 0.29; 'invoke': 0.29; 'subject:like': 0.29; 'fri,': 0.30; 'error': 0.30; 'figure': 0.30; 'located': 0.31; 'code': 0.31; 'file': 0.32; 'could': 0.32; 'print': 0.32; 'interaction': 0.33; 'traceback': 0.33; 'problem': 0.33; 'hi,': 0.33; 'list': 0.35; 'false': 0.35; 'path': 0.35; 'pm,': 0.35; "won't": 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; 'child': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'why': 0.37; 'communicate': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'days': 0.60; 'time,': 0.62; 'is.': 0.62; 'provide': 0.62; 'mentioned': 0.63; 'more': 0.63; 'behavior': 0.64; 'replying': 0.64; 'skip:\xe5 10': 0.65; 'subject:....': 0.65; 'due': 0.66; 'below:': 0.71; '8bit%:94': 0.71; 'as:': 0.75; 'china.': 0.75; 'filling': 0.75; 'late,': 0.84; 'process."': 0.84; 'why?': 0.84
Date Mon, 08 Oct 2012 08:53:46 +0800
From 叶佑群 <ye.youqun@eisoo.com>
User-Agent Mozilla/5.0 (Windows NT 5.2; WOW64; rv:9.0) Gecko/20111222 Thunderbird/9.0.1
MIME-Version 1.0
To Kushal Kumaran <kushal.kumaran+python@gmail.com>
Subject Re: how to run shell command like "<<EOT .... EOT"
References <506555B6.5040406@eisoo.com> <CAH8GtdMW0Eji1LtsafUzY1Av7OASzTxHW6c8KS+3MNg4YvErww@mail.gmail.com> <50664543.1050809@eisoo.com> <CAH8GtdO4LuVQU8tCdJqG4SzT2VxYvH0pWS4Mzrc4HmzB1obNZg@mail.gmail.com>
In-Reply-To <CAH8GtdO4LuVQU8tCdJqG4SzT2VxYvH0pWS4Mzrc4HmzB1obNZg@mail.gmail.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-CM-TRANSID RNOowECJLkcdJHJQKptTAA--.32775S2
X-Coremail-Antispam 1Uf129KBjvJXoWxGry8Jr17ZrWfWw1fXF1rCrg_yoWrXr43pr y2k3WUCr1DGa42yry7t39YvFWSvw4vyrZ3Wrn2qryqyFn0vF1SgFWftr1F9a4Uur4Y9a1j qr90gws3G3W7Xr7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0ziEoGZUUUUU=
X-CM-SenderInfo 11ho50hxtx0q5hlv00oofrz/1tbiChRCSU22IfcrkwAAsA
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1941.1349658559.27098.python-list@python.org> (permalink)
Lines 110
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1349658559 news.xs4all.nl 6840 [2001:888:2000:d::a6]:33486
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:30942

Show key headers only | View raw


于 2012-9-29 19:53, Kushal Kumaran 写道:
> On Sat, Sep 29, 2012 at 6:18 AM, 叶佑群<ye.youqun@eisoo.com>  wrote:
>> 于 2012-9-28 16:16, Kushal Kumaran 写道:
>>> On Fri, Sep 28, 2012 at 1:15 PM, 叶佑群<ye.youqun@eisoo.com>   wrote:
>>>
>>>> Hi, all,
>>>>
>>>>       I have the shell command like this:
>>>>
>>>> sfdisk -uM /dev/sdb<<   EOT
>>>> ,1000,83
>>>> ,,83
>>>> EOT
>>>>
>>>>
>>>>       I have tried subprocess.Popen, pexpect.spawn and os.popen, but none
>>>> of
>>>> these works, but when I type this shell command in shell, it is works
>>>> fine.
>>>> I wonder how to emulate this type of behavior in python , and if someone
>>>> can
>>>> figure out the reason why?
>>>>
>>>>       The sample code of subprocess.Popen is:
>>>>
>>>>       command = ["sfdisk", "-uM",  target, "<<EOT", "\r\n",
>>>>                   ",", 1000, ",", "83", "\r\n",
>>>>                   ",", ",", "83", "\r\n", "EOT", "\r\n"]
>>>>
>>>>       pobj = subprocess.Popen (command, bufsize=1, \
>>>>                           stderr=subprocess.PIPE, stdout=subprocess.PIPE)
>>>>
>>>>       res = pobj.stderr.readline ()
>>>>       if res is not None and pobj.returncode != 0:
>>>>           observer.ShowProgress (u"对设备 %s 分区失败!" % target)
>>>>           return False
>>>>
>>> The "<<EOT" syntax (called a here-document) just provides input to the
>>> command.  If you use the communicate method, you can provide input as
>>> an argument:
>>>
>>> command = ["sfdisk", "-uM",  target ]
>>> instructions = """
>>> ,1000,83
>>> ,,83
>>> """
>>> pobj = subprocess.Popen(command, stdin=subprocess.PIPE,
>>> stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> (output, errors) = pobj.communicate(instructions)
>> I tried this, but it is still not work.
>>
> What do you mean by "not work"?
Sorry for replying so late, these days are long vocation in china.

If I type command in shell line by line, the command will run as 
expected, but when I invoke the command in python, it is always failed. 
Which is what I mean "not work".


>
> - If you get an exception, copy the entire traceback into an email
No exception occured.
>
> - If you do not get an exception, print out the value of the "errors"
> variable to see why the command failed.  You can also check
> pobj.returncode for the exit status of the subprocess.
I solved this problem as below:

fop = os.popen ("sfdisk -uM %s <<EOT\n,%d,83\n,,83\nEOT\n" % (target, fps))

But when I run it with subprocess.Popen (), it doesn't work as os.popen 
even use the code as:

command = ["sfdisk", "-uM",  target ]
instructions = """
,1000,83
,,83
"""

pobj = subprocess.Popen(command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errors) = pobj.communicate(instructions)

stderr.readline () always return the error message means "sfdisk won't accept more than one device parameter except -s or -l is specified", it seems that some parameters were incorrectly treated

>
> A possibility is that you have to replace "sfdisk" with the full path
> to the binary, if it cannot be located on the PATH.  So you will
> replace it with "/usr/sbin/sfdisk", or "/sbin/sfdisk", or wherever the
> file actually is.
>
> <from your other email>
>> If I want to read the output line by line and not put all output to memory buffer in one
>> time, how to write the code?
> You can read line by line by calling pobj.stdout.readline() and
> pobj.stderr.readline().  You can send input to the process by calling
> pobj.stdin.write().  If you manage this interaction "by hand", you
> should not call communicate().  Also, you should be aware of the
> problem mentioned in the subprocess documentation:
>
> "Use communicate() rather than .stdin.write, .stdout.read or
> .stderr.read to avoid deadlocks due to any of the other OS pipe
> buffers filling up and blocking the child process."
>
> Is there any reason why you need to read line-by-line?  You could use
> communicate(), and then call stdout.splitlines() to get a list of
> lines, if that's all you need.
>

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


Thread

Re: how to run shell command like "<<EOT .... EOT" 叶佑群 <ye.youqun@eisoo.com> - 2012-10-08 08:53 +0800

csiph-web