python 下载 ftp 文件

#!/usr/bin/python
# -*- coding: utf-8 -*-
import ftplib
import os
import socket

HOST = 'ops.nginx.com'
DIRN = '/home/ftpuser'
FILE = 'test.txt'


def main():
    try:
        f = ftplib.FTP(HOST)
    except (socket.error, socket.gaierror):
        print 'ERROR:cannot reach " %s"' % HOST
        return
    print '***Connected to host "%s"' % HOST
 
    try:
        f.login('ftpuser','ftpuser')
    except ftplib.error_perm:
        print 'ERROR: cannot login anonymously'
        f.quit()
        return
    print '*** Logged in as "anonymously"'
    try:
        f.cwd(DIRN)
    except ftplib.error_perm:
        print 'ERRORL cannot CD to "%s"' % DIRN
        f.quit()
        return
    print '*** Changed to "%s" folder' % DIRN
    try:
        f.retrbinary('RETR %s' % FILE, open(FILE, 'wb').write)
    except ftplib.error_perm:
        print 'ERROR: cannot read file "%s"' % FILE
        os.unlink(FILE)
    else:
        print '*** Downloaded "%s" to CWD' % FILE
    f.quit()
    return
 
if __name__ == '__main__':
   main()

//修改变量主机IP 目录以及文件名

//修改main函数f.login用户名和密码

python版本基于

[root@db01 ~]# python

Python 2.7.5 (default, Apr 11 201807:36:10)
 

 

posted @ 2020-12-21 16:51  地铁昌平线  阅读(271)  评论(0编辑  收藏  举报