我目前正在尝试通过 pexpect 发送二进制数据。出于某种原因,除了 0x04 之外,数据只是通过 find 得到的,它只是被跳过了。我跟踪了 pexpect 调用以确定所有发生的事情都是对从 pty.fork() 命令打开的文件描述符的 os.write() 调用。
有任何想法吗?
(举例说明问题的示例代码)
import os, pty, sys
pid, child_fd = pty.fork()
if pid: # Parent
os.write(child_fd, b"'\x04hmm\x04'\n")
buf = os.read(child_fd, 100)
print buf
else: # Child
text = sys.stdin.readline()
print ''.join(["%02X " % ord(x) for x in text])
结果:
$ python test.py
'hmm'
27 68 6D 6D 27 0A