python - AttributeError: 'module' object has no attribute 'sendline'
怪我咯
怪我咯 2017-04-17 17:15:54
0
2
1113
怪我咯
怪我咯

走同样的路,发现不同的人生

全部回覆(2)
洪涛

看下面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 上的 Pexpect
4.0 版新增功能:Windows 支援

Pexpect 可以在 Windows 上使用 pexpect.popen_spawn.PopenSpawn 來等待子進程產生模式,或是使用 pexpect.fdpexpect.fdspawn 來等待檔案描述子。目前這應該被認為是實驗性的。

pexpect.spawn 和 pexpect.run() 在 Windows 上不可用,因為它們依賴 Unix 偽終端 (ptys)。跨平台程式碼不得使用這些。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!