开始接触 Pexpect
http://www.noah.org/wiki/pexpect
最近有些时间,就想把一部分工作自动化掉,这部分工作很大一部分是需要处理交互的。一直没有看过 Expect,打算趁这个机会系统学习一下。个人一直喜欢用 Python,就想学习 Python 版本的 Expect —— Pexpect。
一开始以为 Pexpect 就是在 Expect 基础上封装出来的接口,读了介绍性文档后发现自己想错了:
Pexpect is pure Python. Unlike other Expect-like modules for Python Pexpect does not require TCL or Expect nor does it require C extensions to be compiled. It should work on any platform that supports the standard Python pty module.
不过 Pexpect 还是有限制的,需要 Python 带有标准模块 pty,这样 Windows 系统上就不好用了。不过工作什么的一直是在 Linux 上,这也没有什么影响。
pty module Any POSIX system (UNIX) with a working pty module should be able to run Pexpect. The pty module is part of the Standard Python Library, so if you are running on a POSIX system you should have it. The pty module does not run the same on all platforms. It should be solid on Linux and BSD systems. I have taken effort to try to smooth the wrinkles out of the different platforms. To learn more about the wrinkles see Bugs and Testing. Pexpect does not currently work on the standard Windows Python (see the pty requirement); however, it seems to work fine using Cygwin. It is possible to build something like a pty for Windows, but it would have to use a different technique that I am still investigating. I know it's possible because Libes' Expect was ported to Windows. If you have any ideas or skills to contribute in this area then I would really appreciate some tips on how to approach this problem.
感觉挺有意思的。不管怎么说,找到了工具,后边就是实践了。
安装的话,使用 easy_install pexpect 就可以了。如果使用源码包安装的话,解压后 python setup.py install 就可以了。安装很方便。另外,源码包里面还附带了例子程序,这是很不错的学习资源。