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
  Output discrepancy when building Translator skrivver99 19 11,260 Jun-01-2026, 03:11 PM
Last Post: gelum
  Returning numeral output in VS code terminal StephCreatesCode 4 218 Jan-26-2026, 03:45 PM
Last Post: DeaD_EyE
  interactive process for input and output maiya 1 2,300 Mar-27-2025, 08:40 AM
Last Post: Gribouillis
  Rain sensor output only on change Pete6 4 4,769 Mar-10-2025, 06:45 PM
Last Post: ggtux
  How to run shell command, capture the output, then write it into textfile? tatahuft 4 1,828 Dec-20-2024, 02:13 PM
Last Post: Axel_Erfurt
Question [SOLVED] Same input different output antarling 2 1,553 Oct-25-2024, 11:28 PM
Last Post: antarling
  How to send output to stout? natv 9 28,301 Oct-10-2024, 11:06 AM
Last Post: Larz60+
  System showing np.int64(xxx) as output leea2024 3 12,944 Aug-10-2024, 10:55 AM
Last Post: leea2024
  Unexpected output when trying to attach files in a mail application PythonU2Novel 0 1,472 May-17-2024, 02:59 AM
Last Post: PythonU2Novel
  Rich output to PDF HEbO61 1 1,691 Apr-16-2024, 07:30 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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