Read/Write from/to a process

jas codecraig at gmail.com
Mon Oct 24 13:17:39 EDT 2005


Thanks, that is certainly a start.  As you mentioned, the "cd" could is
an issue.

Perhaps checking to see if the line ends with ">" is sufficient?

Dennis Lee Bieber wrote:
> On 24 Oct 2005 07:20:42 -0700, "jas" <codecraig at gmail.com> declaimed the
> following in comp.lang.python:
>
> > Hi,
> >   I would like to start a new process and be able to read/write from/to
> > it.  I have tried things like...
> >
> > import subprocess as sp
> > p = sp.Popen("cmd.exe", stdout=sp.PIPE)
> > p.stdin.write("hostname\n")
> >
> > however, it doesn't seem to work.  I think the cmd.exe is catching it.
>
> 	One: you didn't read any of the "returned" output...
>
> 	Two: the output only seems to be available upon EOF, which means the
> spawned command processor has to exit first... Though you CAN read one
> character at a time, and then have to build lines and expected prompt
> strings...
>
>
> This seems to work:
> -=-=-=-=-=-=-=-=-
> import subprocess
> import os
>
> PROMPT = os.getcwd() + ">"
>
> def getLine(proc):
>     ld = []
>     while True:
>         c = proc.stdout.read(1)
>         if c == "\r": continue  #skip Windows <cr>
>         if c != "\n": ld.append(c) #save all but <lf>
>         if c is None or c == "\n" or c == ">": break
>     ln = "".join(ld)
>     return ln
>
> p = subprocess.Popen("cmd.exe", stdout=subprocess.PIPE, stdin =
> subprocess.PIPE)
>
> print "**** START of captured output"
> while True:
>     ln = getLine(p)
>     print ln
>     if ln == PROMPT: break
> print "**** END of captured output"
>
> p.stdin.write("ipconfig\n")
> print "**** START of captured output"
> while True:
>     ln = getLine(p)
>     print ln
>     if ln == PROMPT: break
> print "**** END of captured output"
>
> p.stdin.write("dir\n")
> print "**** START of captured output"
> while True:
>     ln = getLine(p)
>     print ln
>     if ln == PROMPT: break
> print "**** END of captured output"
>
> p.stdin.write("exit\n")
> -=-=-=-=-=-=-=-=-=-
> E:\UserData\Dennis Lee Bieber\My Documents>python script1.py
> **** START of captured output
> Microsoft Windows XP [Version 5.1.2600]
> (C) Copyright 1985-2001 Microsoft Corp.
>
> E:\UserData\Dennis Lee Bieber\My Documents>
> **** END of captured output
> **** START of captured output
> ipconfig
>
> Windows IP Configuration
>
>
> Ethernet adapter Local Area Connection:
>
>         Connection-specific DNS Suffix  . :
>         IP Address. . . . . . . . . . . . : 192.168.1.100
>         Subnet Mask . . . . . . . . . . . : 255.255.255.0
>         IP Address. . . . . . . . . . . . : fe80::211:11ff:fee1:f303%4
>         Default Gateway . . . . . . . . . : 192.168.1.1
>
> Tunnel adapter Teredo Tunneling Pseudo-Interface:
>
>         Connection-specific DNS Suffix  . :
>         IP Address. . . . . . . . . . . . :
> 3ffe:831f:4004:1956:0:fbde:bd0a:e50b
>         IP Address. . . . . . . . . . . . : fe80::5445:5245:444f%5
>         Default Gateway . . . . . . . . . : ::
>
> Tunnel adapter Automatic Tunneling Pseudo-Interface:
>
>         Connection-specific DNS Suffix  . :
>         IP Address. . . . . . . . . . . . : fe80::5efe:192.168.1.100%2
>         Default Gateway . . . . . . . . . :
>
> E:\UserData\Dennis Lee Bieber\My Documents>
> **** END of captured output
> **** START of captured output
> dir
>  Volume in drive E is Data
>  Volume Serial Number is 2626-D991
>
>  Directory of E:\UserData\Dennis Lee Bieber\My Documents
>
> 10/24/2005  09:23 AM    <DIR>
>           .
> 10/24/2005  09:23 AM    <DIR>
>           ..
> 07/25/2005  10:39 PM    <DIR>
>           .metadata
> 10/06/2005  09:54 AM    <DIR>
>           Ada Progs
> 08/13/2005  02:01 PM    <DIR>
>           Agent Data
> 10/21/2005  09:29 AM           421,820 apress_offer.pdf
> 07/03/2005  11:36 AM               132 cp.py
> 07/17/2005  12:25 PM    <DIR>
>           Cyberlink
> 07/06/2005  09:32 AM           102,400 db1.mdb
> 07/26/2005  12:20 AM            26,614 eclipse_code.xml
> 10/24/2005  01:08 AM    <DIR>
>           Eudora
> 06/24/2005  08:50 PM               667 fake_oosums.ads
> 06/24/2005  08:50 PM               695 fake_oosums.ali
> 09/06/2005  09:01 PM    <DIR>
>           Genealogy
> 07/13/2005  10:56 PM    <DIR>
>           HomeSite
> 05/08/2005  01:05 PM    <DIR>
>           Investing
> 10/21/2005  10:04 PM    <DIR>
>           Java Progs
> 08/04/2005  10:13 PM               162 main.py
> 10/11/2005  10:43 PM    <DIR>
>           My Downloads
> 05/01/2005  10:31 AM    <DIR>
>           My eBooks
> 04/22/2005  12:09 AM    <DIR>
>           My Music
> 07/10/2005  11:43 AM    <DIR>
>           My Pictures
> 06/29/2005  11:55 PM    <DIR>
>           My PSP Files
> 05/23/2005  09:30 AM    <DIR>
>           My Videos
> 05/01/2005  12:49 PM    <DIR>
>           Office Documents
> 06/27/2005  03:19 PM         7,961,778
> org.eclipse.jdt.doc.user.I20050627-1435.pdf
> 06/27/2005  03:19 PM         6,791,109
> org.eclipse.platform.doc.user.I20050627-1435.pdf
> 10/11/2005  10:52 PM                56 oth_tsr_rm_750.ram
> 07/20/2005  09:32 AM           108,457 parkerred15yc.jpg
> 09/03/2005  10:36 PM    <DIR>
>           Python Progs
> 10/20/2005  10:38 PM    <DIR>
>           Quicken
> 07/10/2005  12:09 PM         3,356,248 results.xml
> 06/11/2005  12:03 PM               935 Scout_Ship Database.lnk
> 07/03/2005  12:38 PM    <DIR>
>           Scout_Ship My Documents
> 10/24/2005  09:23 AM               971 Script1.py
> 09/25/2005  12:40 PM             1,107 Script1_old.py
> 08/28/2005  11:47 AM    <DIR>
>           SimpleMu Logs
> 06/24/2005  08:56 PM             1,201 student_pack.ads
> 06/24/2005  08:49 PM             1,144 student_pack.ads.0
> 06/24/2005  08:56 PM             1,342 student_pack.ali
> 08/02/2005  11:39 PM             4,096 t.DOC
> 06/20/2005  10:11 AM               104 t.rx
> 08/05/2005  08:41 PM            66,452 Untitled-1.tif
> 08/05/2005  08:41 PM    <DIR>
>           VCheck
> 10/03/2005  02:58 AM    <DIR>
>           Visual Studio
> 10/03/2005  02:51 AM    <DIR>
>           Visual Studio 2005
>               21 File(s)     18,847,490 bytes
>               25 Dir(s)  267,162,845,184 bytes free
>
> E:\UserData\Dennis Lee Bieber\My Documents>
> **** END of captured output
>
> E:\UserData\Dennis Lee Bieber\My Documents>
>
> -=-=-=-=-=-=-=-=-
>
> 	Note that my "getLine()" has to return on either a real EOL, OR on
> the end of a command prompt ("stuff>"). I also had to initialize the
> prompt at the start. If someone issued a "cd" command to the subprocess,
> the prompt would be all out of sequence, and the code would hang.
>
> 	You'll also note that the prompt /ends/ a capture sequence, and the
> next command is the start of the /next/ capture sequence.
>
> --
>  > ============================================================== <
>  >   wlfraed at ix.netcom.com  | Wulfraed  Dennis Lee Bieber  KD6MOG <
>  >      wulfraed at dm.net     |       Bestiaria Support Staff       <
>  > ============================================================== <
>  >           Home Page: <http://www.dm.net/~wulfraed/>            <
>  >        Overflow Page: <http://wlfraed.home.netcom.com/>        <




More information about the Python-list mailing list