[Tutor] string not found in variable

jark AJ jarkmx at gmail.com
Sun Mar 21 19:59:26 EDT 2021


Thank you so much Dennis and dn for your response.

I am using Python Python 2.7.16

Python Python 2.7.16  Output
___

('Executing Command >>', 'nc -zvw 4 www.google.com 443')
Connection to www.google.com port 443 [tcp/https] succeeded!

<type 'str'>
False
__

___

import subprocess


class Command(object):
    def __init__(self, cmd):
        self.cmd = cmd

    def run(self, shell=False):
        try:
            print("Executing Command >>", self.cmd)
            output = subprocess.Popen(
                self.cmd.split(), stdout=subprocess.PIPE, shell=shell
            )
            return output.communicate()[0]
        except subprocess.SubprocessError as error:
            return error

    def __str__(self):
        return str(self.cmd)


for i in range(5):
    nc_command = Command("nc -zvw 4 www.google.com 443")
    output = nc_command.run()
    print(output)
    print(type(output))
    print("succeeded!" in output)

___

I tested using Python 3.8.3 and there was well, I got the result as
For Python 3 - I had to use universal_newlines=True in the subprocess.popen
to get the result as a string.
However still I see the same result as python 2.7
One strange thing I noticed when tried using Jupyter notebook, the
print(output) is not displayed, however the result are getting logged in
the terminal

[image: image.png]

On Sun, Mar 21, 2021 at 7:30 PM Dennis Lee Bieber <wlfraed at ix.netcom.com>
wrote:

> On Sun, 21 Mar 2021 18:31:19 -0400, jark AJ <jarkmx at gmail.com> declaimed
> the following:
>
>
> >import subprocess
> >
> >class Command(object):
> >    def __init__(self, cmd):
> >        self.cmd = cmd
> >
> >    def run(self, shell=False):
> >        try:
> >            print("Executing Command >>", self.cmd)
> >            output = subprocess.Popen(
> >                self.cmd.split(), stdout=subprocess.PIPE, shell=shell
> >            )
> >            return output.communicate()[0]
> >        except subprocess.CalledProcessError as e:
> >            return e.returncode
> >
> >    def __str__(self):
> >        return str(self.cmd)
> >
>
>         For some reason, I find creating a class, when the methods of that
> class are called in sequence, and then the class instance is disposed of.
>
> >count = 0
> >while count <= 5:
>
>         for _ in range(5):
>
> and get rid of count completely
>
>
> >    nc_command = Command("nc -zvw 4 www.google.com 443")
> >    output = Command.run(nc_command)
>
>         As mentioned above, I'd likely just replace the above with the
> contents
> of the run method, modified to put the actual command in needed places.
>
>         Note: since you created a class instance, normal usage would be
>
>         output = nc_command.run()
>
> >    count += 1
> >    print(output)
> >    print(type(output))
> >    print("succeeded!" in output)
> >
> >___
> >
> >
> >Output:
> >__
> >
> >('Executing Command >>', 'nc -zvw 4 www.google.com 443')
> >Connection to www.google.com port 443 [tcp/https] succeeded!
> >
> ><type 'str'>
>
>         Which version of Python are you running?
>
> >>> output = "Connection to www.google.com port 443 [tcp/https]
> succeeded!\n"
> >>> print(output)
> Connection to www.google.com port 443 [tcp/https] succeeded!
>
> >>> print("succeeded!" in output)
> True
> >>> type(output)
> <class 'str'>                           <<<<<<
> >>>
>
>
> --
>         Wulfraed                 Dennis Lee Bieber         AF6VN
>         wlfraed at ix.netcom.com
> http://wlfraed.microdiversity.freeddns.org/
>
> _______________________________________________
> 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