Not able to read the text using pexpect/expect - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Not able to read the text using pexpect/expect (/thread-35980.html) |
Not able to read the text using pexpect/expect - Bipinjohnson - Jan-06-2022 Hi, I am new to python i am trying some automation where in i want to call a terminal application from python and control the same. I have tried Pexpect where i am able to open the application, but not able to read the contents from the terminal. pasted below is my code: import pexpect child = pexpect.spawn ('./test_app -g',timeout=3,encoding='utf-8') #i am expecting the below string return_val=child.expect(['Enable QSE API? (Y/n)']) #if i receive the same string then i want to pass y to the terminal child.sendline('y') #now i am expecting the below string child.expect('Enable log? (Y/n)') child.sendline("y")When i execute the above script always timeout occurs and gets the below erorr/log message: Something about the application: It runs on Ubuntu 18.x.x. terminal, once its started it will publish a menu and the user have to input the menu options.Note: When i am tying to run the application manually, everything works fine. Looking forward for some guidance from experts. Regards Johnson RE: Not able to read the text using pexpect/expect - lucasbazan - Jan-06-2022 Hii. U can use subprocess lib to start some process. from subprocess import Popen, PIPE process = Popen(['test_app', '-g'], stdout=PIPE, stderr=PIPE) stdout = process.communicate() print(stdout)The list inside Popen class is the arguments that you want to performs. If you want more information you can access https://docs.python.org/3/library/subprocess.html Hope this helps RE: Not able to read the text using pexpect/expect - ibreeden - Jan-07-2022 Hi @Bipinjohnson , In the documentation of expect() you can read: Quote:Strings will be compiled to re types. This means your expected string is a dangerous one because it will be treated as a regular expression: 'Enable QSE API? (Y/n)' The question mark means the previous character must appear 0 or 1 time. The parenthesis also have a meaning of capturing substrings. So you should escape those characters: 'Enable QSE API\? \(Y\/n\)' I am not sure if this works. Play it safe and just check for: 'Enable QSE API' Then also: you do not check if the string is found. You should check the returned value. return_val=child.expect(['Enable QSE API']) #if i receive the same string then i want to pass y to the terminal if return_val == 0: child.sendline('y') else: print("The expected string was not found") sys.exit() RE: Not able to read the text using pexpect/expect - Bipinjohnson - Jan-07-2022 Hi Tim/lucasbazan, Thank you for your response. I tried your suggestion but its not working for me. I gone through the documentation and try adding few more arguments to the code, but still no luck. Pasted below is my code from subprocess import Popen, PIPE process=Popen(['/home/user/Automation/test_app', '-g'], stdout=PIPE, stderr=PIPE, stdin=PIPE, shell=True, universal_newlines=True) print("return value",process) stdout=process.communicate(input='y',timeout=3) print(stdout)while executing the code i am always getting timeout and pasted below is the error message /log. Note: After the error is occurring i am not able to run the script again in the same terminal, may be the process might me running in the background and occupying it. Any other suggestions to solve this? Is there any way to know whether the executable got executed? Or is there any way to see that message("Enable QSE API? (Y/n)" in the terminal itself? RE: Not able to read the text using pexpect/expect - Bipinjohnson - Jan-07-2022 Hi Larz60+, Sorry for my oversight, i will take care of your suggestions hereafter. Regards Johnson RE: Not able to read the text using pexpect/expect - Bipinjohnson - Jan-07-2022 Hi Ibreeden, I tried your suggestion, but looks like its again getting timed out pasted below is my code and its error message. import pexpect child = pexpect.spawn ('./test_app -g', encoding='utf-8',timeout=3) return_val=child.expect(["Enable QSE API? (Y/n)"]) print("After expect",return_val) if return_val==0: child.sendline('y') else: print("The expected string was not found") sys.exit() return_val=child.expect('Enable log? (Y/n)') if return_val==0: child.sendline('y') else: print("The expected string was not found") sys.exit()Error message /log: Why its not even printing the return value? Also in the error message what are those extra characters (\x1b[2J\x1b[1dEnable QSE API? (Y/n) )? Any other thoughts from your side to solve this? RE: Not able to read the text using pexpect/expect - ibreeden - Jan-07-2022 (Jan-07-2022, 06:34 PM)Bipinjohnson Wrote: Also in the error message what are those extra characters (\x1b[2J\x1b[1dEnable QSE API? (Y/n) )?These are probably ANSI terminal escape sequences. Never mind about them, it's a long story. My suggestion was to shorten the expect string in line 4: return_val=child.expect(["Enable QSE API"])This should be enough to recognize the line. If it works you should also strip all characters that activate regular expressions from line 12. Oh and make it also a list by adding square brackets, otherwise the return code may not be zero. And also: Are you sure a timeout of 3 seconds is sufficient? What happens if you leave the timout default (30)? RE: Not able to read the text using pexpect/expect - Bipinjohnson - Jan-10-2022 Hi ibreeden Thanks a lot for that suggestion, it worked by shortening the expected string. Note: it was not the timeout , i have already tried increasing the timeout. Regards Johnson |