Python Forum
Popen - How can I read data before CTRL+C command is issued
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Popen - How can I read data before CTRL+C command is issued
#1
I have a command line tool in windows that scans for devices.

It will loop through all devices in range repeating until it receives a CTRL+C command.

device1 name firmware
device2 name firmware
device3 name firmware
device1 name firmware
device2 name firmware
device3 name firmware
...

I want to stop the scan when it reaches a certain device so I can issue an update firmware command.

At the moment I can only capture the output after the CTRL+C command is issued using the following function that starts the scan, sleeps, then issues the CTRL+C command, I then catch the error and process the output in the except block:

            command = [self.cli_tool, '-s']

            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
            startupinfo.wShowWindow = subprocess.SW_HIDE

            stream = []

            if self.check_dongle_firmware() is not False:
                try:                    
                    self.proc = subprocess.Popen(
                                command, 
                                stdin=subprocess.PIPE, 
                                stdout=subprocess.PIPE, 
                                stderr=subprocess.DEVNULL, 
                                startupinfo=startupinfo)

                    time.sleep(SCAN_TIMEOUT)
                    os.kill(self.proc.pid, signal.CTRL_C_EVENT)
                    self.proc.wait()

                except KeyboardInterrupt:                
                    for line in self.proc.stdout:
                        stream.append(line)							

                    for x in stream[7:]:
                        x = x.decode()
                        print(x.strip())   
I want something like this:
    stream = []

     self.proc = subprocess.Popen(
                                command, stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE, 
                                stderr=subprocess.DEVNULL, 
                                startupinfo=startupinfo)

    for line in self.proc.stdout:
        stream.append(line)	
		if 'device' in line:
			os.kill(self.proc.pid, signal.CTRL_C_EVENT)
			self.proc.wait()
This does not work though.

I tried various combinations using :

universal_newlines=True

bufsize=1

self.proc.communicate()[0]

When I try the code using a command like ping I have no problem and full control of the output.

I have searched SO for tried anything that looks similar but nothing works for what I need.

I think I am missing something obvious or it is not possible due to the tool locking or being in a separate process not accessible by python.

Any direction or advice is appreciated!
Reply


Messages In This Thread
Popen - How can I read data before CTRL+C command is issued - by maffaz - Jun-26-2018, 08:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 587 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Correctly read a malformed CSV file data klllmmm 2 2,204 Jan-25-2023, 04:12 PM
Last Post: klllmmm
  Read nested data from JSON - Getting an error marlonbown 5 1,517 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Read data via bluetooth frohr 9 3,746 Jul-10-2022, 09:51 AM
Last Post: frohr
  Write and read back data Aggie64 6 2,035 Apr-18-2022, 03:23 PM
Last Post: bowlofred
  How to read rainfall time series and insert missing data points MadsM 4 2,316 Jan-06-2022, 10:39 AM
Last Post: amdi40
  Python continues after CTRL-C kjbolhuis 2 1,954 Aug-06-2021, 04:28 PM
Last Post: kjbolhuis
  [Solved] Using readlines to read data file and sum columns Laplace12 4 3,728 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  Looping to read data in database CEC68 1 1,781 Sep-24-2020, 08:54 PM
Last Post: scidam
  Assigning data read from CSV to class faruk61 2 2,193 Apr-15-2020, 05:52 PM
Last Post: buran

Forum Jump:

User Panel Messages

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