Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 26th, 2012, 08:03 AM
thetester thetester is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 thetester User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 38 sec
Reputation Power: 0
Subprocess - then read - then write to same shell?

Please can someone explain how to do this:

I want to make some commands in a shell - read the output and based on the output make another input in the same shell

Code:
import subprocess
p = subprocess.Popen('some_program', stdin = subprocess.PIPE, stdout = subprocess.PIPE)
out, err = p.communicate()

if "abcdefg" in out:
    p.communicate(input = ' -c -d 1234')
    out, err = p.communicate()
    if "xyz" in out:
        print "I have been able to open a shell, read the output and give another command to the same shell"


I have looked at readlines, stdin.write etc but I am lost as to what the solution will be. It seems that as soon as I 'read' the output the process is then closed preventing me from giving any further inputs. If I open a new shell then its no good - because the 2nd commands must come after the 1st (if it meets a condition).

Reply With Quote
  #2  
Old November 26th, 2012, 10:14 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,353 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 8 h 3 m 36 sec
Reputation Power: 383
Others and I solve this problem by searching the subprocess output for a known prompt. At that point we know the shell is ready for new input.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old November 26th, 2012, 10:57 AM
thetester thetester is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 thetester User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 38 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
Others and I solve this problem by searching the subprocess output for a known prompt. At that point we know the shell is ready for new input.


But how do you do that? Once you have read the output it appears to then close it - so that you can't write anything else. What command do you use to search and what are you searching in?

Reply With Quote
  #4  
Old November 26th, 2012, 11:20 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,353 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 8 h 3 m 36 sec
Reputation Power: 383
I can't help it if your program terminates. oh, yes, you could run it in a shell loop. Anyway, I helped some dude or dudette interact with a chess engine about a year ago. Click this underlined text. There are other examples, tell google to search with
Code:
site:forums.devshed.com/python-programming-11/ subprocess b49p23tivg
ha ha there are many hits.

Reply With Quote
  #5  
Old November 27th, 2012, 07:06 AM
thetester thetester is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 thetester User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 38 sec
Reputation Power: 0
Much appreciated for your quick and informative replies!

I have looked through but I'm still doubtful that subprocess can solve my problem - it seems once you search or read the output that the pipe is then closed.

I have however discovered the addon module 'pexpect'.

For example the following works:
Create two python files as below - the first must be called login.py (only because we call that specific file from the test.py main file - which is what we run). Test both cases by first entering correct password (password) and secondly entering an incorrect password ( =! password)

Code:
\\\\\\\\\login.py \\\\\\\\\
passw = raw_input('Enter PASS: ')
if passw == 'password':
    raw_input('You are logged in')
else:
    raw_input('Incorrect login')
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


/////// test.py /////////
import pexpect

child = pexpect.spawn('python login.py')
index = child.expect ('PASS')
if index==0:
    password = raw_input('Please enter your password: ')
    child.sendline (password)

index = child.expect (['logged in', 'Incorrect'])
if index ==0:
    print "Test output ===> you are logged in"
if index ==1:
    print "Test output ===> your password was incorrect"

raw_input('\nPress any key to quit')
\\\\\\\\\\\\\\\\\


I will experiment on my main set up to see if this will do its job in the project im working on.

Reply With Quote
  #6  
Old November 27th, 2012, 02:52 PM
thetester thetester is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 thetester User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 38 sec
Reputation Power: 0
Yes, the above works well when applied to my main project. This has helped me to reach a milestone today!

thanks for comments

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Subprocess - then read - then write to same shell?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap