#!/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.sendline('get robots.txt')
sys.stdout.write(child.before) # 輸出比對'ftp> '之前的輸入與輸出