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

srinivasan srinivasan.rns at gmail.com
Tue Nov 27 04:46:12 EST 2018


Dear All,

I have fixed  the issue with below code snippet for parsing the
command output without try and exception, pls let me know if any
improvements are needed, might be useful for others


    def to_bytes(self, str):
        # Encode to UTF-8 to get binary data.
        if isinstance(str, bytes):
            return str
        return str.encode('utf-8')

    def to_string(self, bytes):
        if isinstance(bytes, str):
            return bytes
        return self.to_bytes(bytes)

    def convert_string(self, bytes):
        try:
            return self.to_string(bytes.decode('utf-8'))
        except AttributeError:  # 'str' object has no attribute 'decode'.
            return str(bytes)
        except UnicodeError:
            return str(bytes)

    def sample(self, ssid, pw):

            cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)

            p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
                                 shell=True)
            out, err = p.communicate()
            out = self.convert_string(out)
            err = self.convert_string(err)

            print("The value of data", out.split(" "))

            print("printing stdout!!!!!!!!!!", out)

            print("printing err!!!!!!!!!!", err)

            print("printing retcode!!!!!!!!!!", p.returncode)

            if p.returncode != 0:
                raise subprocess.CalledProcessError(cmd=cmd,
                                                    returncode=p.returncode,

output="{}\n{}".format(out, err))

            return out

if __name__ == "__main__":
    m = bt()

    print(m.sample("NIassddWiFi", "T.f.o.s.1996!abcdfg"))
On Sun, Nov 25, 2018 at 11:05 PM Steven D'Aprano <steve at pearwood.info> wrote:
>
> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list