#!/usr/bin/env python
#coding:utf-8
from __future__ import unicode_literals # 使用unicode編碼
import pexpect
import sys
child = pexpect.spawnu('ftp ftp.openbsd.org')
child.expect('(?i)name .*: ') # (?i)表示後面的字符串正則匹配忽略大小寫
child.sendline('anonymous')
child.expect('(?i)password')
child.sendline('[email protected]')
child.expect('ftp> ')
child.sendline('bin') # 啓用二進制傳輸模式
child.expect('ftp> ')
child.sendline('get robots.txt')
child.expect('ftp> ')
sys.stdout.write(child.before) # 輸出匹配'ftp> '之前的輸入與輸出
print 'successfully received the file'
child.sendline('bye')