首頁  >  問答  >  主體

python - AttributeError: 'module' object has no attribute 'sendline'

怪我咯怪我咯2742 天前1120

全部回覆(2)我來回復

  • PHP中文网

    PHP中文网2017-04-17 17:17:54

    看下面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

    回覆
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:17:54

    Windows 上的 Pexpect
    4.0 版新增功能:Windows 支援

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

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

    回覆
    0
  • 取消回覆