Python Forum
Unexpected output when searching for a string from os.popen output
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected output when searching for a string from os.popen output
#1
Hello Community,

on a Linux system I try to use nc command within a Python script to check if a UDP port is open or not.

My script:
import os
import re

CHECK = 0

UDPSTATUS = os.popen('nc -vz -u 127.0.0.1 123')
LINE = " "
while LINE:
        LINE = UDPSTATUS.read()
        print LINE
        if LINE.find('succeeded!') != -1:
           print " => UDP PORT OK"
           CHECK = 1
           print CHECK
UDPSTATUS.close()

if CHECK == 0:
        print " !!! UDP PORT NOT OK !!!"
        print CHECK
When the NTP service is running I got:
Output:
Connection to 127.0.0.1 123 port [udp/ntp] succeeded! !!! UDP PORT NOT OK !!! 0
I do not understand why the script does not find "succeeded!".
Can someone please explain why.

Best regards
Reply
#2
By default, subprocess APIs' output is bytestream. Use either
if LINE.find(b'succeeded!')
or
if LINE.decode().find('succeeded!')
or you may open the process with the argument universal_newlines=True - that will turn output format to text stream
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Hello volcano63,

thank you for your reply.

I tested it but it did not work.

Meanwhile I have found a workaround. I write the result to a file and check the content of the file.
I got this to work so no need to inevstigate here further.

Best regards
Reply
#4
(Oct-02-2018, 10:19 AM)FujiJean Wrote: I tested it but it did not work.
Missed that you are on Python2 (why, oh, why?).
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can rich.logging output to file? pyfoo 1 187 Mar-21-2024, 10:30 AM
Last Post: pyfoo
  problem in output of a snippet code akbarza 2 302 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  Reading and storing a line of output from pexpect child eagerissac 1 4,148 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  output shape problem with np.arange alan6690 5 610 Dec-26-2023, 05:44 PM
Last Post: deanhystad
  Error with output djprasanna 1 509 Nov-28-2023, 06:40 PM
Last Post: deanhystad
  [split] Why is there an output of None akbarza 1 416 Nov-27-2023, 02:53 PM
Last Post: deanhystad
  Unexpected output Starter 2 439 Nov-22-2023, 12:08 AM
Last Post: Starter
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 657 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 469 Oct-24-2023, 05:56 AM
Last Post: buran
  output values change akbarza 3 487 Oct-18-2023, 12:30 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020