On 01/21/2010 11:39 AM, Javier Collado wrote:
Hello,
If you set shell=False, then I think that arg2 should be separated
into two different parts.
Also, arg3 could be set just to pattern (no need to add extra spaces
or using str function).
Best regards,
Javier
2010/1/21 Tomas Pelka<tompe...@gmail.com>:
Hey all,
have a problem with following piece of code:
--------------------------------------------------
import subprocess
paattern = "python"
cmd = "/usr/bin/locate"
arg1 = " -i"
arg2 = " -d /var/www/books/mlocate.db"
arg3 = str(" " + pattern)
p1 = subprocess.Popen([cmd, arg1, arg2, arg3], shell=False,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdoutdata, stderrdata) = p1.communicate()
print p1.returncode
print "%s -- %s" % (stdoutdata, stderrdata)
--------------------------------------------------
But return code is always 1 and command do not return any result/error
(stdoutdata, stderrdata are None). If I run this command (/usr/bin/locate -i
-d /var/www/books/mlocate.db python) from standard shell everything goes
fine.
Could you please give me an advice what I'm doing wrong?
Thanks
Cheers
--
Tom
--
http://mail.python.org/mailman/listinfo/python-list
Thanks Javier for advice, but sill same result. I'm running this code as
cgi script from apache. Weird is that when i run it from shell as
apache user, like
----------------
# su -s /bin/bash -c "/usr/bin/locate -i -d /var/www/books/mlocate.db
python; echo $?" apache
0
---------------
I always get "0", but as cgi it returns "1". When I run this script by
other user (tom), I'll obtain nonzero output what is OK.
Additional info:
# su -s /bin/bash -c "ls -l /var/www/books/mlocate.db" apache
-rw-rw-r-- 1 tom books 1465653 Jan 20 13:33 /var/www/books/mlocate.db
so db is readable by apache
Whore source attached.
--
Tom
#!/usr/bin/python
import cgi
import cgitb; cgitb.enable() # for troubleshooting
import subprocess
import sys
import os
sys.stderr = sys.stdout
command = ""
result = ""
stdoutdata = ""
stderrdata = ""
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from field 'pattern'
pattern = form.getvalue('pattern', 'None')
# Get data from field 're'
re = form.getvalue('re')
cmd = "/usr/bin/locate"
arg1 = "-i"
arg2a = "-d"
arg2b = "/var/www/books/mlocate.db"
arg3 = "-r"
arg4 = str(pattern)
p1 = None
if re == "re":
p1 = subprocess.Popen([cmd, arg1, arg2a, arg2b, arg3, arg4],
shell=False, \
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
command = "%s %s %s %s %s %s" % (cmd, arg1, arg2a, arg2b, arg3, arg4)
else:
p1 = subprocess.Popen([cmd, arg1, arg2a, arg2b, arg4], shell=False, \
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
command = "%s %s %s %s %s " % (cmd, arg1, arg2a, arg2b, arg4)
(stdoutdata, stderrdata) = p1.communicate()
print "Content-type: text/html"
print
# debug
print "UID: %i <br>" % os.getuid()
print "Search pattern: %s <br>" % pattern
print """stdout: %s <br>
stderr: %s <br>
""" % (stdoutdata, stderrdata)
print "Return code: %i" % p1.returncode
print """
<html>
<head><title>Hledej v books</title></head>
<p align="center">
<form name="input" action="search.py" method="get">
Hledany vyraz:
<input type="text" name="pattern" />
<input type="submit" value="Hledej" />
<br />
Hledat pomoci regularniho vyrazu?
<input type="checkbox" name="re" value="re" />
<br />
</form>
</p>
<hr>
<br />
"""
if p1.returncode == 0:
if stdoutdata:
result = stdoutdata
else:
result = "Nic takoveho sem nenasel :/"
else:
result = '<font color="red"><b>Chyba</b></font>: \
index souboru je bud zastaraly nebo doslo \
k chybe pri vyhledavani.<br \><small><code>%s</code> \
<br \><code>%s</code></small>' % (command, stderrdata)
print """
<h1>Hledany vyraz "%s" se nachazi v nasledujicich adresarich</h1>
%s
</html>
""" % (pattern, result)
--
http://mail.python.org/mailman/listinfo/python-list