ftp-down.py——利用pexpect實現FTP下載文件

#!/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')


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章