subprocess.call

Jens Thoms Toerring jt at toerring.de
Fri Apr 19 10:43:05 EDT 2013


Ombongi Moraa Fe <moraa.lovetakes2 at gmail.com> wrote:
> [-- text/plain, encoding 7bit, charset: ISO-8859-1, 19 lines --]

> In my python script, I have this:

> command="lynx -dump
> 'phpscript?param1=%s&param2=%s&param3=%s&param4=%s&param5=%s'"%(value1,value2,value3,value4)

> result=subprocess.call(command,shell=True)
> print 'xml message'

> However, the response from running the php script is also printed on output
> screen. I don't want this output.

> How can i ensure that only the last print 'xml response' is returned?

You mean is printed out? Use subprocess.Popen() and redirect
stdout to a pipe, similar to this:

 p = subprocess.Popen(command, stdout=subprocess.PIPE)
 r = p.communicate()
 print r[0]   # This is the output (to stdout)

The return value of the Popen objects communicate() method is
a tuple with two elements, first contains what got written to
stdout (if redirected to a pipe, otherwise Nome), the second
what went to stderr (again if redirected to a pipe). If you
also redirect stdin then you can pass what you want to send
to the process spawned via Popen() as an argument to commu-
nicate().

BTW, according to the dicumentation you should split the
command line into its componenents and pass that as a list
to the call() or Popen() subprocess methods, so it would
seem to reasonable to use e.g.

 command = [ 'lynx', '-dump',
             ( "'phpscript?param1={0}&param2={1}&param3={2}"
			   "&param4={3}&param5={4}'" )
             .format( value1, value2, value3, value4, value5 ) ]


Note that there was one value for creating the string to be
passed to lynx was mising.
                               Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt at toerring.de
   \__________________________      http://toerring.de



More information about the Python-list mailing list