473,766 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Control an external console program

Hi,

I know Python since about 3 days or so and I like it very much :-) I am
currently tying to control an external console driven program but I
have encountered some problems while trying to do so.

Suppose I will try to control a program like a ftp command line client.
At first I wrote something like:

import popen2

fin, fout = popen2.popen2(" ftp")
print "Out: " + fin.readline()

I expected to get an output like "Out: ftp>" but I got not nothing. I
think the reason is that readline() reads a line till it encountered a
"\n" or EOF, but nothing of both happen because the ftp client just
wrote the prompt and is now waiting for input, or not? Therefore I
thought, ok I just write an "\n" to fout and because the ftp client
will echo it I can read a line with readline() and get the prompt. But
it doesn't work either. Now I started asking myself if it just work
this way at all. Instead of just writing a "\n" to fout I wrote
"help\n" and read a line from fin and this time I got the first line of
the output from the help command *yeeha* :-) Unfortunately any
successive read from fin does not work. I can only read the first line
the help command issued, not the 5 or 6 following the first line.

So my question is how do I control a command line tool? I want to
interactively control a command line tool, so I will issue some
commands need the output from the tool and act acording to it. I guess
readline() is not the answer because if there is no line to read
present it just waits. Can anyone give me some hints for writing such a
program? How is the python command line tool integrated in the many
IDE's around? I guess they have the same problem, or not?

Bye
Alex

Jul 18 '05 #1
1 2221
Alexander Stante <al*********@ya hoo.de> wrote:
So my question is how do I control a command line tool? I want to


Check out http://pexpect.sourceforge.net/

martin
Jul 18 '05 #2

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

Similar topics

1
3003
by: Will Stuyvesant | last post by:
I never used the popen or popen2 libraries but it is my understanding that they can capture the output of console based programs. Is it also possible to send keystrokes to console base programs? I would like to program a Python program that can play for instance the tty version of nethack 3.4.3 on Windows, simulating a human player: for nethack it would not be possible to know if a human or a computer program is playing it. It is a...
4
4470
by: Matthew Maylin | last post by:
I'm trying to call a external program from another. My main program is a GUI buildin borland c++ builder, and my sibbling is compiled Matlab code. I would like to call to the program, passing arguments and suppress the msdos window from popping up. I have had success with system( ) and spawnl( ) but cant seem to get the external program to just run in the background.
3
3085
by: Daniel | last post by:
Hi, Does anyone know if it is possible to interact with another console based program. For example, relaying data back and forth between the user and the program. ie. 1. User interacts with C# program. 2. C# program accepts input and sends to console program as if it was std input
9
1763
by: Alvin Bruney [MVP] | last post by:
Exceptions must not be used to control program flow. I intend to show that this statement is flawed. In some instances, exceptions may be used to control program flow in ways that can lead to improved code readability and performance. Consider an application that must eliminate duplicates in a list. using system.collections;
6
1707
by: Dmitry Duginov | last post by:
My web application uses resource files to keep localized strings etc. Those resource files prepared by external console program, which extracts this stuff from the database. Is there a way to invoke that program automatically after I hit build (rebuild) button in Visual Studio, before VSS actually trying to build the solution? I'm working with Whidbey beta 2.
3
1432
by: lotus | last post by:
Hi all. I want to control two different instrtument but have simliar functionality. Acually the fisrt one is controlled by using serial communication, and the other is controlled by LAN communication. The number of instruments will be extetned by 5 or more. So If user select instrument (ex. by selection of combo box items), I must do right procedure of contolling proper instrument.
2
2128
by: Lonifasiko | last post by:
Hi group, I must launch from my Winforms application another application (.exe developed by the client) which response time sometimes can be more or less 30 seconds. I'm using ProcessManager class and WaitForExit() to start it and wait till it rsponds me. It's necessary for the application to get a response, that's why I use WaitForExit(). I don't have access to the external .exe developed by the client,
24
2014
by: tizi_de | last post by:
Hello all, I'm looking for a sample program in C to print out lines not to the standard MS Dos Box but into a different control e.g. text control. Has C the possibility to do printouts to a control instead of using the MS Dos Box? And what control can I use for this purpose when I need to have the most similar look to the MS Dos Box? Every help is greatly appreciated.
1
2319
by: =?Utf-8?B?Q3JhaWc=?= | last post by:
Hi Guys, I am trying to caputre the output for an external application. The idea is to use the System.Diagnostics.Process to run the exe in a process and redirect the output to a string. When I run the external command it works fine. But when i wrap it in a .net exe it fails to exit and the application does not exit. If I kill the external exe from task manager, the .net wrap completes.
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.