[Tutor] string not found in variable
dn
PythonList at DancesWithMice.info
Sun Mar 21 18:40:30 EDT 2021
On 22/03/2021 11.31, jark AJ wrote:
> Hi All,
>
> There are two actions I am trying to achieve here.
>
> 1) To check if the connectivity exists between the source and destination
> 2) To run the test five times to ensure there is no intermittent packet
> loss we experience.
>
> looking to use the nc command here in python, On a bash shell, we do it
> using
> for i in {1..5}; do nc -zvw 4 www.google.com 443 ; done
>
> I have the following questions:
>
> 1) I am storing the subprocess output in a variable called output which is
> of type string. It has succeeded in the output; however, when I use
> print("succeeded!" in output), its returning false
> 2) Is there a way we could keep appending the output like a list in the
> string. trying to do the success/failure logic at the end based on the
> results of all five tests. Looking to achieve this using the variable which
> stores the output.
>
> Thank you in advance!
>
> ___
>
> 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)
>
> count = 0
> while count <= 5:
> nc_command = Command("nc -zvw 4 www.google.com 443")
> output = Command.run(nc_command)
> 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'>
> False
Is it type byte, cf str?
--
Regards,
=dn
More information about the Tutor
mailing list