我正在使用包装器库将命令发送到Windows可执行文件,并且python脚本运行良好。
但是当我用py2exe转换脚本时,我从生成的进程中得到错误,说明相应的命令不存在。
process = subprocess.Popen(args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
有些命令毫发无损地到达,直到其中一个命令缺少第一个字母。
process.stdin.write(data)
process.stdin.flush()
如果是发送命令
未知动作:ommand
修改
class Command(object):
def __init__(self, app, cmdstr):
if isinstance(cmdstr, six.text_type):
cmdstr = cmdstr.encode('ascii')
self.app = app
self.cmdstr = cmdstr
self.status_line = None
self.data = []
def execute(self):
self.app.write(self.cmdstr + b'\n')
while True:
line = self.app.readline()
if not line.startswith('data:'.encode('ascii')):
self.status_line = line.rstrip()
result = self.app.readline().rstrip()
return self.handle_result(result.decode('ascii'))
self.data.append(line[6:].rstrip('\n\r'.encode('ascii')))
def handle_result(self, result):
if result == '' and self.cmdstr == b'Quit':
return
if result == 'ok':
return
if result != 'error':
raise ValueError('received: {0}'.format(result))
msg = b'[no error message]'
if self.data:
msg = ''.encode('ascii').join(self.data).rstrip()
raise CommandError(msg.decode('ascii'))
class Application(object):
executable = 'myApp'
args = []
def __init__(self):
self.sp = None
self.spawn_app()
def spawn_app(self):
args = [self.executable] + self.args
self.sp = subprocess.Popen(
args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
def close(self):
self.sp.kill()
def write(self, data):
self.sp.stdin.write(data)
self.sp.stdin.flush()
def readline(self):
return self.sp.stdout.readline()
def exec_command(cmdstr):
c = Command(app, cmdstr)
c.execute()
return c
# all b'...' work
exec_command(b'Quit')
exec_command(b'Enter')
# this seems to work as well
command = 'Connect({0})'.format(host).encode('ascii')
exec_command(command)
# those two don't
exec_command('Move({0}, {1})'.format(y, x).encode('ascii'))
exec_command('String("{0}")'.format(text).encode('ascii'))