Issue in using "subprocess.Popen" for parsing the command output
srinivasan
srinivasan.rns at gmail.com
Sun Nov 25 12:54:11 EST 2018
Hope now I have changed on the string output as below, could you please
correct me if am still wrong?
import sys
import subprocess
interface = "wlan0"
def main(ssid, pw):
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=True)
raise AssertionError(s)
#return proc.strip().decode("utf-8")
* return proc.decode("utf-8").strip()*
main("Apartment 18", "40672958689850014685ad")
Error:
/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/venv/bin/python
/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py
Traceback (most recent call last):
File
"/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py",
line 30, in <module>
main("Apartment 18", "40672958689850014685")
* File
"/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py",
line 28, in main*
* return proc.decode("utf-8").strip()*
*AttributeError: 'Popen' object has no attribute 'decode'*
printing stdout!!!!!!!!!!
printing retcode!!!!!!!!!! 0
Process finished with exit code 1
On Sun, Nov 25, 2018 at 11:19 PM MRAB <python at mrabarnett.plus.com> wrote:
> On 2018-11-25 17:13, srinivasan wrote:
> > Dear Python Experts Team,
> >
> > As am newbie still learning the python syntax from past 2 weeks, Excuse
> me,
> > If this might be silly question, As I am trying to execute shell command
> > (ie, nmcli) using "subprocess.Popen".
> >
> > 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.
> >
> > 2. As I am trying to execute shell commands using "subprocess.Popen", I
> am
> > trying to parse the strings output by "cmd = "nmcli device wifi connect
> > '%s' password '%s'" % (ssid, pw)" command as below, but it is throwing
> the
> > below error as shown in "Output error logs:"
> >
> > Could you please let me to fix the bug in the below code snippet,
> where I
> > need the collect the strings of the command output and later how to be
> > parsed after execution of the command for example, I need to parse the
> > string "Connection activation failed: " and compare it with the command
> > output, could you please help me how this can be achieved?
> >
> > *Command:*
> > :~$ nmcli device wifi connect 'Apartment 18' password
> > '40672958689850014685abcdf'
> > Error: Connection activation failed: (7) Secrets were required, but not
> > provided.
> > :~$
> >
> > *Code:*
> > *import sys*
> > *import subprocess*
> >
> > *interface = "wlan0"*
> >
> >
> > *def main(ssid, pw):*
> >
> > * # 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)*
> >
> > * 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)*
> >
> > * return proc.strip().decode("utf-8")*
> >
> > *main("Apartment 18", "40672958689850014685")*
> >
> > *Output error logs:*
> >
> > /home/srinivasan/Downloads/wifidisconnectissuenov23/qa/venv/bin/python
> >
> /home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py
> > Traceback (most recent call last):
> > File
> >
> "/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py",
> > line 38, in <module>
> > printing stdout!!!!!!!!!!
> > printing retcode!!!!!!!!!! 0
> > main("Apartment 18", "40672958689850014685")
> > File
> >
> "/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py",
> > line 36, in main
> > return proc.strip().decode("utf-8")
> > AttributeError: 'Popen' object has no attribute 'strip'
> >
> > Process finished with exit code 1
> >
> > Kindly do the needful as am stuck with this issue from 2 days
> >
> > Many Thanks in advance,
> >
> Look carefully at the traceback. It's actually telling you what the
> problem is.
>
> You're trying to do .strip() on proc, but proc is the process itself.
>
> Instead, what you want is to do .strip() on the string that it output.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list