[Tutor] subprocess outputing wrong info to command line

Steven D'Aprano steve at pearwood.info
Sat Feb 21 03:58:20 CET 2015


On Fri, Feb 20, 2015 at 06:58:17PM -0500, brads wrote:

> My subprocess is in error but the same command at the command line works
> fine.

Are you running the Python script as the same user and from the same 
location as the command line? If you are running the Python script as an 
unprivileged user from your home directory, and the command line as root 
from the directory holding the key, then it is not surprising that they 
will get different results.

Does the dnssec-signzone command use any environment variables? Perhaps 
they are not being inherited by the Python subprocess.

Also, a note about sending code to the list. For some reason, every line 
in your code is separated by blank lines:

> # cat makekeys.py
> 
> #!/usr/bin/python3.4
> 
> import subprocess
> 
> import sys

etc. That makes it very hard to read. Please fix that.

Secondly, you have a huge amount of extraneous code which is irrelevant 
to your problem with subprocess. You calculate dates, ask the user for 
input, delete files, and write new files, calculate arrays and more. 
None of that has anything to do with subprocess. Take it out. Reduce 
your problem to the smallest possible code which shows the problem. 9 
times out of 10, the process of cutting your code down to manageable 
size will actually help you to solve your own problem, and the other 1 
time out of 10 we have a smaller and less confusing piece of code to try 
to understand.

You also have dead, obsolete code commented out. Comments should not be 
used for keeping history in the file, comments should be used for 
commenting about the code. Best practice is to use a revision control 
system like hg, but if you're not up to that at least delete the dead 
code before posting.

When running into a problem with subprocess, your first step should be 
to *exactly* duplicate the successful command:

dnssec-signzone -e20180330000000 -p -t -g -k Ktest123.com.ksk.key -o
  test123.com test123.com.external Ktest123.com.zsk.key


So you should try running that from subprocess:

subprocess.call([
    'dnssec-signzone', '-e20180330000000', '-p', '-t', '-g', 
    '-k', 'Ktest123.com.ksk.key', '-o', 'test123.com', 
    'test123.com.external', 'Ktest123.com.zsk.key',
    ])


and see if it still works. If it still does not work, that strongly 
suggests a problem with the environment: you are running it as the wrong 
user, in the wrong location, missing enviromnent variables or 
permissions. If it works in Python, then you can start replacing each 
constant argument with a calculated argument, *one argument at a time*, 
and see where you introduce the bug.



-- 
Steve


More information about the Tutor mailing list