473,408 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Subprocess with a Python Session?

No matter what I do I cant get the following code to do what I expect.
I hadn't used subprocess t o read and write to pipes of a
still-running app, and I just can't seem to get it right. What gives?

import subprocess

p = subprocess.Popen("python", stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write('print 10\n')
assert p.stdout.readline() == '10\n'

--
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://ironfroggy-code.blogspot.com/
Dec 5 '06 #1
1 1558
Calvin Spealman <ir********@gmail.comwrote:
No matter what I do I cant get the following code to do what I expect.
I hadn't used subprocess t o read and write to pipes of a
still-running app, and I just can't seem to get it right. What gives?

import subprocess

p = subprocess.Popen("python", stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write('print 10\n')
assert p.stdout.readline() == '10\n'
To read and write to a still running app, you'll want to use pexpect
probably.

http://pexpect.sourceforge.net/
>>import pexpect
p = pexpect.spawn("python")
p.expect(">>>")
0
>>p.sendline("print 10\n")
10
>>p.readline()
' print 10\r\n'
>>p.readline()
'10\r\n'
>>>
Note that running python under pexpect puts it into interactive
mode.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Dec 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stewart Midwinter | last post by:
this has me puzzled; I've created a small test app to show the problem I'm having. I want to use subprocess to execute system commands from inside a Tkinter app running under Cygwin. When I...
6
by: Ernesto | last post by:
I'm opening a telnet session with the subprocess call below. I then wait ten seconds and attempt to terminate the telnet window I created. Unfortuantely, the telnet window remains after the...
5
by: Cameron Laird | last post by:
Question: import subprocess, StringIO input = StringIO.StringIO("abcdefgh\nabc\n") # I don't know of a compact, evocative, and # cross-platform way to exhibit this behavior. # For now, depend...
1
by: yichun.wei | last post by:
Perl has the ability to do the following: print <<EOF; ...reams of text goes here... EOF Is there a Python equivalent of the above Perl code? This thread has previous discussion on the...
5
by: Grant Edwards | last post by:
I'm trying to use the py-gnuplot module on windows, and have been unable to get it to work reliably under Win2K and WinXP. By default, it uses popen(gnuplotcmd,'w'), but in some situations that...
15
by: Thomas Dybdahl Ahle | last post by:
Hi, When I do a small program like from subprocess import Popen popen = Popen() from time import sleep sleep(100) start it and kill it, the ping process lives on. Is there a way to ensure...
6
by: Eric_Dexter | last post by:
I am having trouble contolling vim with subprocess on a windows machine. It appears that vim comes up on the machine all right and it sometimes looks like it is doing the searchs what I am asking...
7
by: timw.google | last post by:
Hi I want to write a python script that runs rsync on a given directory and host. I build the command line string, but when I try to run subprocess.call(cmd), or p=subprocess.Popen(cmd,...
12
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.