Python Forum
Collecting Output from Pexpect
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Collecting Output from Pexpect
#1
I'm using pexpect to provide login information for a long running command. That part is working. However, I then want to display the output from that command as it progresses. I am trying the following code where proc is a pexpect.spawn object:
 while True:
                try:
                        output=proc.readline(timeout=300)
                        print(output)
                except:
                        continue
While I know the command is outputting lines, the readline never returns anything, it just keeps timing out. How do I retrieve the command output as it progresses? I'm looking for the equivalent to stdout.PIPE used by popen. TIA.
Reply
#2
Maybe you could do something like this
import sys

timeout = 300
timeoutMax = 1000
while True:
    try:
        output = proc.readline(timeout=timeout)
        while not output:
            timeout += 5
            output = proc.readline(timeout=timeout)
            if timeout == timeoutMax:
                print('Timout max reached')
                sys.exit()
    except:
        continue
Reply
#3
Thanks but I know lines have been output before the 300 seconds expires. For some reason, which is the question, it simply is not returning or reading those lines. I'm assuming that it will return something when it detects each new line character.
Reply
#4
I'm still looking for a solution to my problem. When I know what to expect I can get everything with proc.before and proc.after. The problem is that I only get that output when the expected data is received. What I want is to get any output generated by the command, line by line.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading and storing a line of output from pexpect child eagerissac 1 4,221 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Use pexpect to send user input alisha17 0 1,888 May-10-2022, 02:44 AM
Last Post: alisha17
  Not able to read the text using pexpect/expect Bipinjohnson 7 4,028 Jan-10-2022, 11:21 AM
Last Post: Bipinjohnson
  Sudden Problem with pexpect gw1500se 3 2,386 Nov-19-2021, 11:21 PM
Last Post: bowlofred
  How to use pexpect in python? tiho_bg 1 1,525 Oct-30-2021, 02:50 PM
Last Post: Yoriz
  Pexpect timesout before executing whole output eagerissac 0 1,492 Jun-23-2021, 03:30 AM
Last Post: eagerissac
  pexpect startup help korenron 2 3,477 Apr-27-2021, 07:23 AM
Last Post: korenron
  Problem with pexpect.exception.TimeOUT korenron 0 3,292 Apr-12-2021, 03:25 PM
Last Post: korenron
  Request help on pexpect rsurathu 4 5,793 Jul-19-2020, 03:35 PM
Last Post: snippsat
  SIGWINCH ignored when trying to start a screen with the pexpect module StatTark 0 1,827 Jul-06-2020, 10:12 AM
Last Post: StatTark

Forum Jump:

User Panel Messages

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