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


Groups > comp.lang.python > #72218

Re: passing Python assignment value to shell

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <cameron@cskk.homeip.net>
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; 'syntax': 0.04; 'subject:Python': 0.06; 'assignment': 0.07; 'string': 0.09; 'arguments': 0.09; 'etc).': 0.09; 'sanity': 0.09; 'tmp': 0.09; '"%s"': 0.16; '"f"': 0.16; '>the': 0.16; 'above)': 0.16; 'argument.': 0.16; 'check.': 0.16; 'code?': 0.16; 'doing:': 0.16; 'emit': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'invalid.': 0.16; 'message- id:@cskk.homeip.net': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'simpson': 0.16; 'skip:> 20': 0.16; 'syntax,': 0.16; 'wrote:': 0.18; 'skip:f 30': 0.19; 'seems': 0.21; 'command': 0.22; 'shell': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'string,': 0.24; 'cheers,': 0.24; '(or': 0.24; 'pass': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'leave': 0.29; 'went': 0.31; 'dropped': 0.31; 'probably': 0.32; 'run': 0.32; 'open': 0.33; 'running': 0.33; 'really': 0.36; 'method': 0.36; 'useful': 0.36; 'charset:us-ascii': 0.36; 'skip:- 20': 0.37; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'middle': 0.60; 'number,': 0.60; 'first': 0.61; 'content-disposition:inline': 0.62; 'yourself': 0.78; 'received:192.168.15': 0.84
Date Thu, 29 May 2014 15:10:24 +1000
From Cameron Simpson <cs@zip.com.au>
To python-list@python.org
Subject Re: passing Python assignment value to shell
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii; format=flowed
Content-Disposition inline
In-Reply-To <92C18466-C23B-4AB9-9863-540A8FB6E3E3@gmail.com>
User-Agent Mutt/1.5.21 (2010-09-15)
References <92C18466-C23B-4AB9-9863-540A8FB6E3E3@gmail.com>
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10438.1401340238.18130.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1401340238 news.xs4all.nl 2856 [2001:888:2000:d::a6]:56811
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72218

Show key headers only | View raw


On 28May2014 21:48, Satish Muthali <satish.muthali@gmail.com> wrote:
>This is what I went about doing:
>
>    reecalc = [s.split() for s in os.Popen("free -ht").read().splitlines()]

I think you dropped an "f" in your cut/paste. Try to be precise.

>    freecalc_total = freecalc[4]
>    freecalc_total = freecalc_total[3]
>    freecalc_total = freecalc_total.translate(None, 'M )

This is syntacticly invalid. Did this really come from working code?
Also, .translate does not accept None as its first argument.

>   tmp = "%s" % freecalc_total

If freecalc_total is a string (as it seems from the above) then this line does 
nothing.

If freecalc_total were a number, it would probably be better to use "%d" 
instead of "%s"; %d will emit an error if freecalc_total is not a number, a 
useful sanity check.

>  command = "stressapptest -M %s -s 20" % tmp
>  p = subprocess.Popen(command, shell=True,  stdout=subprocess.PIPE, stderr=
>subprocess.PIPE).communicate()[0]
>
>Please let me know if this is the optimal method to achieve in order to pass
>the assignment value to shell. I am open to suggestions or a better way of
>implementation/logic.

It is better to avoid shell=True unless you really need to run a shell command 
(shell syntax or control structures, etc). This is because by plonking "%s" in 
the middle of your shell string, you leave yourself open to "injection" 
accidents (or attacks, of you can be tricked in the values you substitute).

Since you are running a command with known arguments and no shell syntax, you 
do not need the shell. So just construct the arguments directly:

   [ "stressapptest", "-M", str(freecalc_total), "-s", "20" ]

and set shell=False.

Cheers,
Cameron Simpson <cs@zip.com.au>

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


Thread

Re: passing Python assignment value to shell Cameron Simpson <cs@zip.com.au> - 2014-05-29 15:10 +1000

csiph-web