[Tutor] Issue in using "subprocess.Popen" for parsing the command output

Steven D'Aprano steve at pearwood.info
Sun Nov 25 17:03:04 EST 2018


I think you are sending email using Gmail. If so, there is a command in 
Gmail to send only PLAIN TEXT with no added formatting. Please use it. 
Your code at the moment has extra asterisks * added at the beginning and 
end of each line.

More comments below.


On Sun, Nov 25, 2018 at 10:43:10PM +0530, srinivasan wrote:

> 1. Am trying to improve the below code with "try" and "exception", could
> you please help me how "try" and "exception" can be used on the below code
> snippet. I hope in my code with try and exception, seems to be a bug.

As a beginner, you should normally not use try...except to report 
errors. You should learn how to diagnose errors by reading the 
traceback. Covering up the traceback with try...except makes debugging 
harder.

Your use here:


> *    try:*
> *        cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)*
> *        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)*
> *        stdout, stderr = proc.communicate()*
> *        retcode = proc.returncode*
> *        print("printing stdout!!!!!!!!!!", stdout)*
> *        print("printing retcode!!!!!!!!!!", retcode)*
> *    except subprocess.CalledProcessError as e:*
> *        s = """While executing '{}' something went wrong.*
> *                        Return code == '{}'*
> *                        Return output:\n'{}'*
> *                        """.format(cmd, e.returncode, e.output, shell=enable_shell)*
> *        raise AssertionError(s)*

doesn't seem right to me. The string.format() method doesn't take a 
shell=enable_shell agument, so I expect that line 

    s = """...""".format(cmd, ..., shell=enable_shell)

to fail. But even if it doesn't fail, the next line:

    raise AssertionError(s) 

is an abuse of exceptions. The failure here is *not* an assertion, and 
you shouldn't use AssertionError. You wouldn't use TypeError or 
UnicodeEncodeError or AttributeError. "AssertionError" should not be 
used for "some arbitrary error".

There are almost no reasons to manually raise AssertionError, except 
perhaps in test frameworks like unittest. Normally you should only get 
an AssertionError from the "assert" command:

https://import-that.dreamwidth.org/676.html

My opinion is, you should remove that try...except altogether. I don't 
think that it helps your code, even if it worked. Calls to Popen can 
fail in many, many ways, and it seems pointless to single out just one 
of them and to replace the useful traceback and error message with a 
less accurate one.


> *Command:*
> :~$ nmcli device wifi connect 'Apartment 18' password
> '40672958689850014685abcdf'
> Error: Connection activation failed: (7) Secrets were required, but not
> provided.

If you cannot get nmcli working directly from the command line, you have 
*no hope* of getting it working with Python getting in the way.

*First* you must be able to run the command directly from the shell, 
with no errors. Then you can move the *working* command to Python and 
Popen.

>     return proc.strip().decode("utf-8")
> AttributeError: 'Popen' object has no attribute 'strip'

The error should explain exactly what the problem is. You are tying to 
call the STRING METHOD string.decode on a Popen object. Did you read the 
error message?

I don't know how to fix it because I don't know what you are trying to 
do.


-- 
Steve


More information about the Tutor mailing list