How to read data using popen

0 votes

Doubt

I have written below code and its working fine. But i need to read particular line of strings from window after passing below inputs.

After reading the data again i need to pass the value. How can i do it?

Please find the attached below code and screen shot.

command = os.path.join(self.skynet_exec_dir, self.skynet_exe)
s = subprocess.Popen(command)
 prcoutput = s.communicate(input = [hotkey('ctrl', 'r'), press('enter'), press('enter'), press('enter'), press('enter'), press('enter'), press('y'), press('enter')])

Aug 19, 2019 in Python by anonymous
4,518 views
Do you mean you want to read the data that has been displayed on the window? Can you share the complete code?
I can't share it in public. please send a mail to my id : vijayk9596@gmail.com

Please find the below code.

I have one exe file that i am running through subprocess.Popen(). I need pass inputs to exe file for registration of system.

Here i am able to pass all inputs. If we are passing already registered name, then it ask one dynamic question.

Test Station with <name> is already registered. Do you want to continue?

If yes then i need to pass hot "Y" else other button.

I got stuck in this line "Test Station with <name> is already registered. Do you want to continue?" This line i have to read and pass value. Can you help me to do it?

You can refer screen shot attached in the main question.

command = os.path.join(self.skynet_exec_dir, self.skynet_exe)
s = subprocess.Popen(command)
(prcoutput, prcerr) = s.communicate(input = [hotkey('ctrl', 'r'), press('enter'), press('enter'), press('enter'), press('enter'), press('enter'), press('y'), press('enter')])

It is difficult to understand without the code. I can see in the screenshot that the input is not being taken after the line Test Station with <name> is already registered. Do you want to continue?. Is that what you mean by "stuck"? If yes, have you written the code to take input? 

P.S.: I suggest you at least share the code by masking personal data/information. Because there might be a problem in the logic

Hi karan, 

Yes you are correct Input is not being taken.

Test Station with <name> is already registered. Do you want to continue?

This line displays only if already registered system exists otherwise it won't ask this question. Here i need to read this line because i need to pass input.

If the system name is not exists it will not ask any question and no need of passing any inputs.

I need logic to handle this.

if any other thing other then Popen for this situation.

Please find the full code. 

def auto_registartion_skynet(self, machine_name, machine_pool):
        self.stop_skynet()
        time.sleep(2)
        self.skynet_info_dat = "INFO.DAT"
        kybrd = Controller()

        try:
            command = os.path.join(self.skynet_exec_dir, self.skynet_info_dat)
            msg = "%s: reading INFO.DAT file: %s" %(self.__class__.__name__, command)
            print(msg)

            if os.path.exists(command):
                with open(command, "r") as fr:
                    data = fr.readline()
                    temp_data = data.split(";")

            temp_data[2] = "TestStationName" + "=" + machine_name
            temp_data[4] = "TestStationPool" + "=" + machine_pool

            result = ";".join(temp_data)

            msg = "%s: Deleting the INFO.DAT file: %s" %(self.__class__.__name__, command)
            print(msg)
            os.remove(command)

            msg = "%s: Creating new INFO.DAT file with user input: %s" %(self.__class__.__name__, command)
            print(msg)

            if not os.path.exists(command):
                with open(command, "w") as fw:
                    fw.write(result)
            
        except FileNotFoundError as e:
            trc_bck = traceback.format_exc()
            msg = "Exception : %s\n Traceback: %s" %(str(e), str(trc_bck))
            print(msg)

        command = os.path.join(self.skynet_exec_dir, self.skynet_exe)
        s = subprocess.Popen(command)
        (prcoutput, prcerr) = s.communicate(input = [hotkey('ctrl', 'r'), press('enter'), press('enter'), press('enter'), press('enter'), press('enter'), press('y'), press('enter')])

        print(prcoutput)


Can you try this:

command = os.path.join(self.skynet_exec_dir, self.skynet_exe)
s = subprocess.Popen(command, prcoutput=PIPE, prcerr=PIPE)
(prcoutput, prcerr) = s.communicate(input = [hotkey('ctrl', 'r'), press('enter'), press('enter'), press('enter'), press('enter'), press('enter'), press('y'), press('enter')])

Hi Esha,

This didn't work. Because 'prcoutput' is not reserved argument. Below error is coming.

Traceback (most recent call last):
  File "Skynet.py", line 150, in <module>
    appTest.auto_registartion_skynet("BL-IN-KVM-11", "FCH")
  File "Skynet.py", line 143, in auto_registartion_skynet
    s = subprocess.Popen(command, prcoutput=PIPE, prcerr=PIPE)
TypeError: __init__() got an unexpected keyword argument 'prcoutput'

Can you try stdout and stderr instead?

s = subprocess.Popen(command, stdout=PIPE, stderr=PIPE)

Yes. Not worked.

But i got solution. 

I just did like this

        command = os.path.join(self.skynet_exec_dir, self.skynet_exe)
        proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
        
        while proc.poll() is None:
            output = proc.stdout.readline()
            if 'Executor is Ready' in str(output):
                hotkey('ctrl', 'r')
            elif 'Enter Test Station Unique Identifier' in str(output):
                press('enter')
            elif 'Enter Test Station Location' in str(output):
                press('enter')
            elif 'Enter Test Station Space ID' in str(output):
                press('enter')
            elif 'Enter Test Station Preferred Orchestrator Host Name' in str(output):
                press('enter')
            elif 'Enter Test Station Pool' in str(output):
                press('enter')
            elif 'Confirm update' in str(output):
                press('y')
                press('enter')
            elif 'is already registered' in str(output):
                press('y')
                press('enter')
            elif 'REGISTER SUSCCESSFUL' in str(output):
                return True
                break
            print(output)

This is working for me. I wanted like non-blocking approach so i just used Popen. process.communicate() blocks the execution so i was not able to read the content from the window.

Thank you everyone for helping me.

Hey @Vijay. I'm glad it worked. Can you just post the solution as the answer? It will be helpful for other people looking for the solution. Thanks.

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

0 votes
1 answer

How to read data from a text file using Python?

Refer to the below example where the ...READ MORE

answered May 13, 2019 in Python by Sushma
1,268 views
+3 votes
5 answers

How to read multiple data files in python

Firstly we will import pandas to read ...READ MORE

answered Apr 6, 2018 in Python by DeepCoder786
• 1,720 points
14,788 views
+1 vote
1 answer

How to read hdfs file using python?

subprocess.Popen(["hadoop", "fs", "-cat", "/path/to/myfile"], stdou ...READ MORE

answered Dec 7, 2018 in Python by Omkar
• 69,210 points
5,403 views
0 votes
1 answer

How to use read a WSDL file from the file system using Python suds?

Hi, good question. It is a very simple ...READ MORE

answered Jan 21, 2019 in Python by Nymeria
• 3,560 points
7,709 views
0 votes
1 answer
0 votes
0 answers

How to read data in a bubble sort algorithm in python?

I am not able to read the ...READ MORE

Jul 31, 2019 in Python by Waseem
• 4,540 points
873 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,058 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP