473,749 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to control a console based program from Python

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 console based program, it
accepts keystrokes and outputs characters on the (Windows) console
box. I did see that WConio can read characters from the screen,
perhaps I can use that, have to experiment with it some more. But how
to send keystrokes? Any help and ideas are much appreciated. And a
happy new year of course!
Jul 18 '05 #1
1 3002
|Thus Spake Will Stuyvesant On the now historical date of Sat, 03 Jan 2004
02:24:32 -0800|
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 console based program, it accepts
keystrokes and outputs characters on the (Windows) console box. I did
see that WConio can read characters from the screen, perhaps I can use
that, have to experiment with it some more. But how to send keystrokes?
Any help and ideas are much appreciated. And a happy new year of
course!


The fact that you're on a microsoft box makes the issue of console-based
programs significantly more sticky than it would be on a *nix system.

You may know this already, but in case you don't, here's a bit of
technical info to get you oriented.

Unix was, early on, designed around the idea of pipes. Pretty much every
program reads from stdin and writes to stdout. Thus, there's a constant
flow of data into the program and a constant flow out. On *nix systems, a
terminal pushes your keystrokes to stdin, and interprets the results it
gets from stdout. (This description is oversimplified, but it will do.)
The results contain certain codes that, depending on the terminal, move
the cursor around, change colors and such. A good resource on all this
is: http://www.faqs.org/docs/artu/index.html

Even if you're not that interested in unix stuff, it's a good read because
Python inherits a lot of the unix philosophy.

MS-DOS, and hence microsoft console programs, were based on an entirely
different philosophy, that of CP/M. Whereas Unix was designed for big
computers with many users running many programs at once, CP/M was designed
for computers with one user running only one program. CP/M simply
abstracted the low-level hardware calls through the use of interrupts.
Interrupts are called interrupts because they do exactly that: they
interrupt the entire computer, run some low-level code, then return
control to the (one and only) program running. I don't think pipes were
ever part of CP/M, but at some point they were grafted onto MS-DOS. (This
was probably to make some grumpy unix wizards complain less vocally about
having to write code for single user machines.) Almost nothing useful is
done with pipes in MS-DOS. When you run a console program in Windows,
it's a special program that pretends to be the old Dos system. Apropos
Docs:
http://www.wkonline.com/d/CPM.html
http://www.wkonline.com/d/MS-DOS.html
Chances are that the nethack you're running displays to the screen via
these crufty old Dos interrupts. While it is possible to "steal" these
interrupts (and snag their data) I don't know of any libraries that would
let you do that, I wouldn't try it unless you've got some serious sorcery
in you. To give you an idea of how complex it is, somewhere around here,
I've got a manual on all those interrupts and I think it's about 6 inches
thick and involves manipulating the processor registers directly. You can
probably find some c code to do that, but umm... I dunno. I always just
inlined assembly when I was working with MS-Dos.
(;Please, god no ABEND NOW!
MOV AH,4Ch
MOV AL,01h
INT 21h ;*weeps* Fifteen years later, and I still remember that.
)
http://users.win.be/W0005997/GI/dosref.html

If you *really* want to try making a robot that plays nethack, I suggest
installing cygwin, which gives you a unix environment on top of your
windows system, then compiling nethack for it and *then* you'll have
access to proper pipes. Besides, you'll get to dabble a bit in *nix land.
You might like it.
http://www.cygwin.com/

Sam Walters

p.s. You might try taking this to the python-tutor list. You will
probably get more answers to questions like this.

--
Never forget the halloween documents.
http://www.opensource.org/halloween/
""" Where will Microsoft try to drag you today?
Do you really want to go there?"""

Jul 18 '05 #2

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

Similar topics

1
2518
by: Hugh | last post by:
I am using python 2.3 through the PythonWin program on windows. I would like to create a console-based interactive session. The program raw_input is almost exactly what I'd like, except that whenever I call raw_input(), it pops up a window on my screen. I'd much rather have it read from the interactive window. Is there something out there, as easy as raw_input(), that I can use? Thanks for any clues!
6
5483
by: Lucas Raab | last post by:
I'm looking to play a joke on a friend and I'm wondering if there's a way to not show or hide the DOS console. My friend doesn't have python so I have to compile it to an EXE. TIA
1
2219
by: Alexander Stante | last post by:
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
1
3765
by: frank | last post by:
Hi all I don't think this is strictly a Python problem, but as it manifests itself in one of my Python programs, I am hoping that somebody in this group can help me. The following is a message I sent to co.os.linux.setup - "My question concerns line graphics on a text-based console. ­My actual problem relates to a program I have written using
9
1762
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;
24
2012
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.
3
9734
by: Daniel Clark | last post by:
I have a Windows command line based application that only shuts down cleanly if it sees "CTRL-C" on the console. I need to automate the running of this application, but still allow the user sitting at the machine to cancel the process cleanly if he/she needs to. In Unix this would be a tiny shell script that used "kill -15", but under Windows there does not seem to be an easy way to do this, at least that I can find. Below is a test...
7
1531
by: tylerca | last post by:
I'm attempting to start some process control using Python. I've have quite a bit of literature on networking, and have made some tinkering servers and clients for different protocols HTTP, FTP, etc... But now it's time for the murky web of industrial protocol. I'm looking to start with IO and servo controls via Ethernet. Questions: Is there an existing forum on this already? What protocols are the most python friendly? i.e. are...
3
9054
by: Scott | last post by:
I have a requirement to control a firefox web browser from an external python program. The python program running under linux from a command shell needs to first find all open firefox web browser windows read the URL currently displayed in each web browser and if the URL matches a particular regular expression it needs to get/set form fields displayed in the web browser. Basically I need something like Windows COM and Internet Explorer...
0
8997
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
8833
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
9389
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...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
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...
0
6079
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
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
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.