At 11:49 PM 3/29/2005 -0500, Chris Maloof wrote:
Hello,

I'm trying to read the output from a WinXP console application using PythonWin -- that is, I start the application as a child process, and I want to be able to read the ASCII text in the application's screen.

I wrote some pipes code to do walkie-talkie with an old FROTRAN exe (that used extended ASCII) using win32pipe.
It reads from the DOS box first, then sends input in response.
I tried pipedream.py, but had issues with threads and the delays from the remote DOS. It is worth reading though, at least.


Ray Schumacher

===========================================================

# process module
import os
import sys
import time

from wxPython.wx import *
import string
import re

def escapifyString(str):
    """escapifyString(string) -> string
    Return the given string with any non-printing ASCII characters escaped."""
    pattern1 = re.compile('\[..\;..H')
    pattern2 = re.compile('\[2J')
    l = list(str)
    vis = string.letters + string.digits + " _!$%^&*()-=+[]{};'#:@~,./<>?|`"
    for i in range(len(l)):
        if l[i] not in vis: l[i] = '' #"\\%03o" % ord(l[i])
    str = string.join(l, "")
    str = re.sub(pattern1, '', str)
    str = re.sub(pattern2, '', str)
    return str


def getDataFrom(pipe): done = False timeout = 3 ## seconds start_time = time.time() data = '' error = None while not done: try: if(os.fstat(pipe.fileno())[6]!=0): ## this will read up to a large amount data = os.read(pipe.fileno(), 2**16) elif(len(data)): ## if the read was done on the last loop, we can quit done = True elif((time.time() - start_time) > timeout): ## something went wrong ## or, request was done at the end of the process done = True error = 'timeout error:'+ str(time.time() - start_time) print 'Error; read timed out' else: ## might be starting up the remote process... time.sleep(.01) except (IOError, OSError): print '(IOError, OSError)',(IOError, OSError) done = True return [error, data]

def driveRemote(stdinput, stdoutput, resp):
""" takes pipes and a string to send"""
## first, get any data available
timeOut, result = getDataFrom(stdoutput)
if timeOut: return 'error'
print "from:'%s'\n" % escapifyString(result)[-60:],
if (result==('' or None)):
dlg = wxMessageDialog(parent, "resp not received!"+"\nGot '"+result+"'",
'Error', wxOK | wxICON_INFORMATION)
try:
dlg.ShowModal()
finally:
dlg.Destroy()
#pgDlg.Destroy()
return 'error'
## check for run-time error
if (string.find(result, 'run-time error')>-1):
dlg = wxMessageDialog(None, result+'\n, please wait',
'run-time error', wxICON_ERROR)
try:
dlg.ShowModal()
print 'run-time error'
while (string.find(result, 's!!')>-1):
#os.write(stdinput.fileno(), '\n')
#timeOut, result = getDataFrom(stdoutput)
print result
finally:
dlg.Destroy()
return 'error'
if resp!=None:
## send a command
try:
print 'response :', resp, '\n'
os.write(stdinput.fileno(), str(resp)+'\n')
except IOError, e:
print 'error; resp, reslt:', resp, result
print "IOError %s" % e
return result




=============================================================
# calling module:
import win32pipe
import Process
import string
import time
import os.path
    ## actually start remote DOS
    stdin, stdout = win32pipe.popen4("remote.EXE", 't')

    result = Process.driveRemote(stdin, stdout, '') ## Enter (or Return)

_______________________________________________
Python-win32 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to