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
  interactive process for input and output maiya 1 583 Mar-27-2025, 08:40 AM
Last Post: Gribouillis
  Rain sensor output only on change Pete6 4 3,713 Mar-10-2025, 06:45 PM
Last Post: ggtux
  How to run shell command, capture the output, then write it into textfile? tatahuft 4 972 Dec-20-2024, 02:13 PM
Last Post: Axel_Erfurt
Question [SOLVED] Same input different output antarling 2 914 Oct-25-2024, 11:28 PM
Last Post: antarling
  How to send output to stout? natv 9 26,739 Oct-10-2024, 11:06 AM
Last Post: Larz60+
  System showing np.int64(xxx) as output leea2024 3 6,592 Aug-10-2024, 10:55 AM
Last Post: leea2024
  Unexpected output when trying to attach files in a mail application PythonU2Novel 0 1,039 May-17-2024, 02:59 AM
Last Post: PythonU2Novel
  Rich output to PDF HEbO61 1 953 Apr-16-2024, 07:30 AM
Last Post: Pedroski55
  Can rich.logging output to file? pyfoo 1 2,253 Mar-21-2024, 10:30 AM
Last Post: pyfoo
  problem in output of a snippet code akbarza 2 1,256 Feb-28-2024, 07:15 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