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

2018-11-27 Thread srinivasan
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  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 

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

2018-11-25 Thread Cameron Simpson

On 26Nov2018 09:03, Steven D'Aprano  wrote:

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.


Very true, but...


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)*

[...]

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". [...]
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.


I'd add one qualificaion here: it may be that he wants to report this 
exception in particular, while still not "handling it". In which case 
I'd advocate something like:


 try:
   ... Popen stuff ...
 except subprocess.CalledProcessError as e:
   s = 
   print(s, file=sys.stderr)
   raise

i.e. report some special message, then _reraise_ the original exception.

In this way he gets to keep the original exception and traceback for 
debugging, which still making whatever special message he wanted to 
make.


Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2018-11-25 Thread Steven D'Aprano
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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2018-11-25 Thread Mats Wichmann
On 11/25/18 10:58 AM, srinivasan wrote:
> Even only with "*proc.decode("utf-8")"* in the above code still it seems to
> throw the error
> 
> #return proc.strip().decode("utf-8")
> #return proc.decode("utf-8").strip()
> *return proc.decode("utf-8")*
> 
> Error:
> /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/venv/bin/python
> /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py
> printing stdout!!
> printing retcode!! 0
> Traceback (most recent call last):
>   File
> "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py",
> line 31, in 
> main("Apartment 18", "40672958689850014685")
> *  File
> "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py",
> line 29, in main*
> *return proc.decode("utf-8")*
> *AttributeError: 'Popen' object has no attribute 'decode'*

the error tells you what is wrong.  proc is a Popen object, and does not
have a decode method.  simple enough.  strings have a decode method.
You're decoding the wrong thing, you need to decode what you get back
from communicating with the Popen object, not the Popen object itself.

stdin is the string (or more likely, in Python 3, a bytes object) you
got back from calling communicate(). decode that.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2018-11-25 Thread srinivasan
Even only with "*proc.decode("utf-8")"* in the above code still it seems to
throw the error

#return proc.strip().decode("utf-8")
#return proc.decode("utf-8").strip()
*return proc.decode("utf-8")*

Error:
/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/venv/bin/python
/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py
printing stdout!!
printing retcode!! 0
Traceback (most recent call last):
  File
"/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py",
line 31, in 
main("Apartment 18", "40672958689850014685")
*  File
"/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py",
line 29, in main*
*return proc.decode("utf-8")*
*AttributeError: 'Popen' object has no attribute 'decode'*

Process finished with exit code 1


On Sun, Nov 25, 2018 at 11:24 PM srinivasan 
wrote:

> 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 
> 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  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)*
>> > *

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

2018-11-25 Thread srinivasan
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 
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,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor