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')


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