473,387 Members | 1,575 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,387 software developers and data experts.

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 2972
|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
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...
6
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
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...
1
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...
9
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...
24
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...
3
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...
7
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,...
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...

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.