[python] python subprocess vraci porad 1
Tomas Pelka
tompelka na gmail.com
Čtvrtek Leden 21 14:33:20 CET 2010
On 01/21/2010 11:52 AM, Jan Jakubuv wrote:
> zdravim,
>
> On Thu, Jan 21, 2010 at 09:44:55AM +0100, Tomas Pelka wrote:
>> Dobry napad, predelal sem tedy na:
>>
>> --------------------------------------------------
>> cmd = "/usr/bin/locate"
>> arg1 = " -i"
>> arg2 = " -d /var/www/books/mlocate.db"
>> arg3 = str(" " + pattern)
>>
>
> Chyba je formatu argumentu arg1, atd. Ta mezera na zacatku tam nesmi byt.
> Zkuste:
>
> arg1 = "-i"
> arg2a = "-d"
> arg2a = "/var/www/books/mlocate.db"
> arg3 = str(pattern)
>
> p1 = subprocess.Popen([cmd, arg1, arg2a, arg2b, arg3], shell=False, ...)
>
> Zbytek uz je spravne, pokud argumenty predavate jako list pak je skutecne
> treba zadat je oddelene.
>
> Pokud chcete zadat prikaz jako jeden string pak pouzijte `shell=True` a
> namistu listu zadejte string:
>
> p1 = subprocess.Popen("/usr/bin/locate -i -d file pattern", shell=True, ...)
>
>> (stdoutdata, stderrdata) = p1.communicate()
>>
>> print p1.returncode
>> print "%s -- %s" % (stdoutdata, stderrdata)
>> --------------------------------------------------
>>
>> Ale stale vraci 1 a zadny vysledek.
>>
>
> honza.
>
>
>
Diky za radu ale stale se nedokazu dobrat nejakemu vysledku.
Skript je vlastne CGI spoustene z apache, divne ale je, ze kdyz spustim
z shellu:
----------------
# su -s /bin/bash -c "/usr/bin/locate -i -d /var/www/books/mlocate.db
python; echo $?" apache
0
---------------
tak vzdy dostanu 0 ale zadny vysledek, pritom CGI vraci 1. Kdyz to same
spustim jako bezny uzivatel (ne apache) tak dostanu 0 a nenulovy tedy
spravny vysledek.
Problem s mlocate.db by byt nemel, protoze je citelny pro apache (ten je
ve skupine books) a vlastne i pro ostatni, viz:
----------------
# 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
----------------
Cely CGI v priloze.
--
Tom
------------- další část ---------------
#!/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)
Další informace o konferenci Python