走同样的路,发现不同的人生
看下面dir的結果,pexpect模組裡面沒有sendline這個方法,所以會報AttributeError.
>>> import pexpect >>> dir(pexpect) ['EOF', 'ExceptionPexpect', 'Expecter', 'PY3', 'TIMEOUT', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__revision__', '__version__', 'exceptions', 'expect', 'is_executable_file', 'pty_spawn', 'run', 'runu', 'searcher_re', 'searcher_string', 'spawn', 'spawnbase', 'spawnu', 'split_command_line', 'sys', 'utils', 'which']
正確的用法範例如下:
# This connects to the openbsd ftp site and # downloads the recursive directory listing. import pexpect child = pexpect.spawn('ftp ftp.openbsd.org') child.expect('Name .*: ') child.sendline('anonymous') child.expect('Password:') child.sendline('noah@example.com') child.expect('ftp> ') child.sendline('lcd /tmp') child.expect('ftp> ') child.sendline('cd pub/OpenBSD') child.expect('ftp> ') child.sendline('get README') child.expect('ftp> ') child.sendline('bye')
要在pexpect.spawn(cmd)的回傳值上呼叫sendline方法,參考:http://pexpect.readthedocs.org/en/stable/overview.html
Windows 上的 Pexpect4.0 版新增功能:Windows 支援
Pexpect 可以在 Windows 上使用 pexpect.popen_spawn.PopenSpawn 來等待子進程產生模式,或是使用 pexpect.fdpexpect.fdspawn 來等待檔案描述子。目前這應該被認為是實驗性的。
pexpect.spawn 和 pexpect.run() 在 Windows 上不可用,因為它們依賴 Unix 偽終端 (ptys)。跨平台程式碼不得使用這些。
看下面dir的結果,pexpect模組裡面沒有sendline這個方法,所以會報AttributeError.
正確的用法範例如下:
要在pexpect.spawn(cmd)的回傳值上呼叫sendline方法,參考:http://pexpect.readthedocs.org/en/stable/overview.html
Windows 上的 Pexpect
4.0 版新增功能:Windows 支援
Pexpect 可以在 Windows 上使用 pexpect.popen_spawn.PopenSpawn 來等待子進程產生模式,或是使用 pexpect.fdpexpect.fdspawn 來等待檔案描述子。目前這應該被認為是實驗性的。
pexpect.spawn 和 pexpect.run() 在 Windows 上不可用,因為它們依賴 Unix 偽終端 (ptys)。跨平台程式碼不得使用這些。