how to control an application using python script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • learnerofpython
    New Member
    • Nov 2006
    • 14

    how to control an application using python script

    I have a cygwin application which i have invoked using python script like:
    os.system("path of the exe file")
    How can i enter text in the cygwin( which has appeared )using the python script itself???
  • fuffens
    New Member
    • Oct 2006
    • 38

    #2
    Are you running Python directly from Windows or from CygWin? In case you run Python from Windows you probably need to use COM to send text to the CygWin window. The win32com module can be used (included in the PythonWin distribution). Look at other posts here for more information.

    I would suggest that you build Python on CygWin instead (or download Python built for CygWin) and then you can make any os.system call you want to instead of sending text to the CygWin window.

    Best regards
    /Fredrik

    Comment

    • kudos
      Recognized Expert New Member
      • Jul 2006
      • 127

      #3
      Can you do something like this?
      Code:
      import os
      
      fd = os.popen("your program that requires user input","w")
      fd.write("some input\n")
      fd.close()
      (or have I completely misunderstood the question?)

      -kudos

      Originally posted by learnerofpython
      I have a cygwin application which i have invoked using python script like:
      os.system("path of the exe file")
      How can i enter text in the cygwin( which has appeared )using the python script itself???

      Comment

      • learnerofpython
        New Member
        • Nov 2006
        • 14

        #4
        thanks for all ur valuable suggestions.
        Whenever i use popen to invoke cygwin application or any other application(e.g a simple notepad) the application is not appearing on the screen


        eg.
        import os

        fd = os.popen("C:\cy gwin\cygwin.bat ","w")
        fd.write("telne t 101.29.34.132")
        fd.close()

        i am unable to view any changes this program makes

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by learnerofpython
          thanks for all ur valuable suggestions.
          Whenever i use popen to invoke cygwin application or any other application(e.g a simple notepad) the application is not appearing on the screen


          eg.
          import os

          fd = os.popen("C:\cy gwin\cygwin.bat ","w")
          fd.write("telne t 101.29.34.132")
          fd.close()

          i am unable to view any changes this program makes
          Nope, that won't work. The object of popen() must have an input (like sys.stdin) and an output (like sys.stdout), so using a batch file to invoke the process won't work. Check out 2.4 docs section 6.9 for examples.

          Comment

          • kudos
            Recognized Expert New Member
            • Jul 2006
            • 127

            #6
            try this...

            Code:
            import os
            input,output= os.popen2("some command","t")
            input.write("some input")
            print output.readline()
            input is like file write(w) part, while output is the file read part(r)

            -kudos

            Comment

            • learnerofpython
              New Member
              • Nov 2006
              • 14

              #7
              no kudos, it didnt work.
              barton i cudnt find section 6.9 in doc 2.4
              please any1 help me out

              Comment

              • fuffens
                New Member
                • Oct 2006
                • 38

                #8
                Here is a really lame solution to your problem. I do not have CygWin installed on my computer so I open notepad and send text to it instead. I think you can make it work for CygWin.

                Code:
                from win32com.client import *
                sh = Dispatch("Wscript.Shell")
                sh.Run("notepad")
                sh.AppActivate("Untitled");sh.Sendkeys("Hello!");sh.SendKeys("{Enter}")
                Kodus, thank you for pointing to the popen function in the os module. I think that is the correct way to go for a stable, general solution. I could not however get it to work. I will do some reading on popen later.

                BR
                /Fredrik

                Comment

                • fuffens
                  New Member
                  • Oct 2006
                  • 38

                  #9
                  ...and here is one for the command window also...

                  Code:
                  from win32com.client import *
                  import time
                  sh = Dispatch("Wscript.Shell")
                  sh.Run("cmd"); time.sleep(1);sh.Sendkeys("dir");sh.SendKeys("{Enter}")

                  Comment

                  • kudos
                    Recognized Expert New Member
                    • Jul 2006
                    • 127

                    #10
                    hmmm...It might be true that telnet won't work with popen. When I think about it, I vaugly remember trying log into telnet via popen (in C, but I guess that is basically the same function/method). It think it had something to do with security (i.e. not allowing people to make scripts with password databases). Anyway, im sitting on a windows machine, and I have no longer an account where I can login from telnet, so I won't be able to test it...

                    -kudos

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #11
                      Originally posted by learnerofpython
                      no kudos, it didnt work.
                      barton i cudnt find section 6.9 in doc 2.4
                      please any1 help me out
                      I tried to paste the pages, but the site wouldn't let me (ME, the moderator)...

                      Comment

                      Working...