【发布时间】:2010-10-10 18:42:18
【问题描述】:
我目前正在尝试通过 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
【问题讨论】:
-
另外,pty 驱动程序正在为您执行本地回显,正如在所有其他操作之前看到
'hmm' 所证明的那样。