# [ Python 文章收集 ] python Pexpect
# Mastering Python Networking 2nd Edition (Packt)
此模組常用函數如下 :
我在 Win10 上的 Python 3.6.1 版安裝 pexpect 過程如下 :
Python 3.6.1 (C:\Python36\python.exe)
C:\Users\user>pip3 install pexpect
Collecting pexpect
Downloading https://files.pythonhosted.org/packages/89/e6/b5a1de8b0cc4e07ca1b305a4fcc3f9806025c1b651ea302646341222f88b/pexpect-4.6.0-py2.py3-none-any.whl (57kB)
Collecting ptyprocess>=0.5 (from pexpect)
Downloading https://files.pythonhosted.org/packages/d1/29/605c2cc68a9992d18dada28206eeada56ea4bd07a239669da41674648b6f/ptyprocess-0.6.0-py2.py3-none-any.whl
Installing collected packages: ptyprocess, pexpect
Successfully installed pexpect-4.6.0 ptyprocess-0.6.0
可見 pexpect 相依於 ptyprocess 模組, 離線安裝必須同時下載 ptyprogress 與 pexpect 的 whl 安裝檔, 而且要先安裝 ptyprogress 再安裝 pexpect. 但安裝完後呼叫 pexpect.spawn() 欲建立 telnet 連線卻出現錯誤訊息 :
>>> import pexpect
>>> child=pexpect.spawn("telnet 127.0.0.1")
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
AttributeError: module 'pexpect' has no attribute 'spawn'
用 Python 內建函數 dir() 檢視 pexpect 模組之內容, 發現裡面並無 spawn() 方法 :
>>> dir(pexpect)
['EOF', 'ExceptionPexpect', 'Expecter', 'PY3', 'TIMEOUT', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__revision__', '__spec__', '__version__', 'exceptions', 'expect', 'is_executable_file', 'searcher_re', 'searcher_string', 'split_command_line', 'sys', 'utils', 'which']
搜尋上面之錯誤訊息才知 pexpect 目前僅支援 Linux/Unix 系列作業系統, 還沒有完全支援 Windows, 參考 :
# Cannot import name 'spawn' for pexpect while using pxssh
# pxssh import issue #339
我在樹莓派安裝 pexpect 後用 dir() 檢視發現在 Linux 上確實可用 :
pi@raspberrypi:~ $ sudo pip3 install pexpect (務必要用 sudo)
Collecting pexpect
Downloading https://files.pythonhosted.org/packages/89/e6/b5a1de8b0cc4e07ca1b305a4fcc3f9806025c1b651ea302646341222f88b/pexpect-4.6.0-py2.py3-none-any.whl (57kB)
Collecting ptyprocess>=0.5 (from pexpect)
Downloading https://files.pythonhosted.org/packages/d1/29/605c2cc68a9992d18dada28206eeada56ea4bd07a239669da41674648b6f/ptyprocess-0.6.0-py2.py3-none-any.whl
Installing collected packages: ptyprocess, pexpect
Successfully installed pexpect-4.6.0 ptyprocess-0.6.0
pi@raspberrypi:~ $ python3
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
>>> dir(pexpect)
['EOF', 'ExceptionPexpect', 'Expecter', 'PY3', 'TIMEOUT', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__revision__', '__spec__', '__version__', 'exceptions', 'expect', 'is_executable_file', 'pty_spawn', 'run', 'runu', 'searcher_re', 'searcher_string', 'spawn', 'spawnbase', 'spawnu', 'split_command_line', 'sys', 'utils', 'which']
D:\PortablePython37>python -m pip install pexpect
Collecting pexpect
Downloading https://files.pythonhosted.org/packages/89/e6/b5a1de8b0cc4e07ca1b305a4fcc3f9806025c1b651ea302646341222f88b/pexpect-4.6.0-py2.py3-none-any.whl (57kB)
Collecting ptyprocess>=0.5 (from pexpect)
Downloading https://files.pythonhosted.org/packages/d1/29/605c2cc68a9992d18dada28206eeada56ea4bd07a239669da41674648b6f/ptyprocess-0.6.0-py2.py3-none-any.whl
Installing collected packages: ptyprocess, pexpect
Successfully installed pexpect-4.6.0 ptyprocess-0.6.0
但後來在 Portable Python 3.7.0 上離線安裝 pexpect (有相依模組 ptyprocess) :
Microsoft Windows [版本 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.
C:\Python37>python -m pip install ptyprocess-0.6.0-py2.py3-none-any.whl
Processing c:\python37\ptyprocess-0.6.0-py2.py3-none-any.whl
Installing collected packages: ptyprocess
Successfully installed ptyprocess-0.6.0
C:\Python37>python -m pip install pexpect-4.6.0-py2.py3-none-any.whl
Processing c:\python37\pexpect-4.6.0-py2.py3-none-any.whl
Requirement already satisfied: ptyprocess>=0.5 in c:\python37\app\python\lib\sit
e-packages (from pexpect==4.6.0) (0.6.0)
Installing collected packages: pexpect
Successfully installed pexpect-4.6.0
卻發現它有與樹莓派上一樣完整的功能 :
>>> import pexpect
>>> dir(pexpect)
['EOF', 'ExceptionPexpect', 'Expecter', 'PY3', 'TIMEOUT', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__revision__', '__spec__', '__version__', 'exceptions', 'expect', 'is_executable_file', 'pty_spawn', 'run', 'runu', 'searcher_re', 'searcher_string', 'spawn', 'spawnbase', 'spawnu', 'split_command_line', 'sys', 'utils', 'which']
所以 Pythoin 3.7 似乎可用 pexpect 了.
參考 :
# Python安装使用命令行交互模块pexpect的基础教程
沒有留言 :
張貼留言