return code of 256 from python

12,340

UPDATE: or maybe just use /usr/bin/sudo /bin/date ... to make sure the commands are found.

Try this:

import subprocess

p = subprocess.Popen('sudo -S date --set ...', shell=True, stdin=subprocess.PIPE)
p.communicate(input='<your password>')

This is a more proper way of launching a subprocess (via the shell), and sending it some input.

If you also need to read the output of the process, then for example:

p = subprocess.Popen('sudo -S date --set ...', shell=True,
                     stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate(input='<your password>')
print "\n".join("out: " + x for x in out.split('\n'))
print "\n".join("err: " + x for x in err.split('\n'))

...or just take the contents of out and/or err and parse them.

Share:
12,340
mitim
Author by

mitim

Updated on June 28, 2022

Comments

  • mitim
    mitim almost 2 years

    This is a reposted question from raspberrypi.stackexchange.com. While I am trying to get something to work on python on the raspberry pi, since it doesn't involve any pi-specific things, it was suggested by someone I post here instead. Original post is here.


    I am trying to make a web ui to change the date in the rapsberry pi, but I keep getting a return code of 256.

    Currently what I have goes like this:

    web page -> submits an ajax request to a python script python checks what type of command (a time/date command in this case) and pieces together a string looking like:

    sudo date --set="20130901 20:10"
    

    and stores it in a variable commandString. Then python goes:

    os.system(commandString)
    

    and the return value is passed all the way up to the web ui where it is printed out.

    I also currently return the commandString value to the web ui too to verify it and it looks okay.

    The problem is that every time I test, I keep getting back 256 as the error return code. The date on the raspberry pi of course doesn't change as I manually check it before and after.

    However, if I manually go in to python on the raspberry pi and try:

    commandString = 'sudo date --set="20130901 20:10"'
    os.system(commandString)
    

    It works with no issues. If I try it without sudo then I get a return value of 256 as well, so I thought maybe it was a permissions issue with my original script. I tried this link to check my script's permissions and it seems to be okay? (os.geteuid() is 0)

    If it matters, I am using lighttpd and fastcgi to run python from a web ui. My lighttpd config is currently:

    fastcgi.server = (
        ".py" => (
        "python-fcgi" => (
        "socket" => "/tmp/fastcgi.python.socket",
        "bin-path" => "/var/www/command.py",
        "check-local" => "disable",
        "max-procs" => 1)
        )
    )
    

    Any ideas on what I'm missing?


    On the original post, it was also suggested I try something like:

    echo <password> | sudo -S date --set="20130829 02:02
    

    While it's probably not a good idea to put in my root password like that, I tried it and got the same result: it works when doing in the terminal/shell and within the python interpreter, but not via the web ui to python.