How to control a console based program from Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Will Stuyvesant

    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!
  • Samuel Walters

    #2
    Re: How to control a console based program from Python

    |Thus Spake Will Stuyvesant On the now historical date of Sat, 03 Jan 2004
    02:24:32 -0800|
    [color=blue]
    > 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![/color]

    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:




    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.
    )


    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.


    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.

    """ Where will Microsoft try to drag you today?
    Do you really want to go there?"""

    Comment

    Working...