using subprocess module in Python CGI

ANURAG BAGARIA anurag.bagaria at gmail.com
Thu Jan 8 05:38:01 EST 2009


Dear Matt,

Thank you for your answer.
This script is just a kind of test script so as to actually get it started
on doing any simple job. The actual process would be much more complicated
where in I would like to extract the tar file and search for a file with
certain extension and this file would be given as input to another program
installed on the server. Later on I would also like to use process.wait() so
that I can get a status of the job execution and completion from the server
and this information can be displayed to the users on the web-page where
they submit their jobs. As to what I understand subprocess.call() would be
the best in that case. Please correct, if I am wrong.

The whole process is like a user submitting a tar file via the web-browser
with some data and getting back the processed results in the form of a new
tar file after performing a few operations on the files submitted as input
tar file.

Thanking you once again for your valuable time.
Regards.

On Wed, Dec 24, 2008 at 1:54 AM, Matt Nordhoff
<mnordhoff at mattnordhoff.com>wrote:

> ANURAG BAGARIA wrote:
> > Hello,
> >
> > I am a Python Newbie and would like to call a short python script via
> > browser using a CGI script, but initially I am trying to call the same
> > python script directly through python command line. The script intends
> > to perform a few command line in a pipe and I have written the script (a
> > short one) as follows.
> >
> > #!/usr/bin/python
> >
> > import cgi, string, os, sys, cgitb, commands, subprocess
> > import posixpath, macpath
> > #file = "x.tar.gz"
> > #comd = "tar -xf %s" % (file)
> > #os.system(comd)
> > #commands.getoutput('tar -xf x.tar.gz | cd demo; cp README ../')
> > comd = [\
> >         "tar -xf x.tar.gz", \
> >         "cd demo", \
> >         "cp README ../", \
> >       ]
>
> That's not how subprocess.call() works. You're trying to run an
> executable called "tar -xf x.tar.gz", passing it the arguments "cd demo"
> and "cp README ../".
>
> > outFile = os.path.join(os.curdir, "output.log")
> > outptr = file(outFile, "w")
> > errFile = os.path.join(os.curdir, "error.log")
> > errptr = file(errFile, "w")
> > retval = subprocess.call(comd, 0, None, None, outptr, errptr)
> > errptr.close()
> > outptr.close()
> > if not retval == 0:
> >         errptr = file(errFile, "r")
> >         errData = errptr.read()
> >         errptr.close()
> >         raise Exception("Error executing command: " + repr(errData))
> >
> >
> > but after trying to execute this independently, I get the following
> > error which I am unable to interpret :
> >
> > Traceback (most recent call last):
> >   File "process.py", line 18, in <module>
> >     retval = subprocess.call(comd, 0, None, None, outptr, errptr)
> >   File "/usr/lib/python2.5/subprocess.py", line 443, in call
> >     return Popen(*popenargs, **kwargs).wait()
> >   File "/usr/lib/python2.5/subprocess.py", line 593, in __init__
> >     errread, errwrite)
> >   File "/usr/lib/python2.5/subprocess.py", line 1135, in _execute_child
> >     raise child_exception
> >
> >
> > Could someone suggest where am I going wrong and if corrected, what is
> > the probability of this script being compatible with being called
> > through the browser. Thanking you people in advance.
>
> Well, you'd need to output something, but otherwise, sure, why not?
>
> print "Content-Type: text/html"
> print
> print "<html>...</html>"
>
> > Regards.
>
> Why do you even need to use subprocess to do this? All it's doing is
> extracting the README file from a tarball, right? You can use the
> tarfile module for that.
>
> <http://docs.python.org/library/tarfile.html>
> --
>



-- 
I just want to LIVE while I'm alive.


AB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090108/9c599019/attachment.html>


More information about the Python-list mailing list