1. 程式人生 > >2.4 利用FTP服務器下載和上傳目錄

2.4 利用FTP服務器下載和上傳目錄

logs conn lean 連接 int edi dir ack ftp服務器

利用FTP服務器下載目錄

import os,sys
from ftplib import FTP
from mimetypes import guess_type

nonpassive = False                                                         #passive FTP by default
remotesite = 192.168.191.1
remotedir = .                                                            #FTP的路徑
remoteuser = ()                                                            #
因為我沒設置密碼,所以為空集 localdir = . #本地路徑 clean_all = input( Clean local directory first? )[:1] in [y,Y] #是否清除本地目錄所有文件 #連接PFTP print(connecting...) connection = FTP(remotesite) connection.login(*remoteuser) connection.cwd(remotedir)
if nonpassive: connection.set_pasv(False) #most servers do passive #清除 if clean_all: for localname in os.listdir(localdir): try: print(deleting local,localname) os.remove(os.path.join(remotedir,localname))
except: print(cannot delete, localname) count = 0 remotefiles = connection.nlst() #只能下載目錄中的文件,不能下載目錄中的目錄 for remotename in remotefiles[:5]: if remotename in (.,..) or not . in remotename:continue #判斷是否目錄,這裏根據實際情況更改 mimetype,encoding = guess_type(remotename) mimetype = mimetype or ?/? mimetype = mimetype.split(/)[0] localpath = os.path.join(localdir,remotename) print(downing,remotename,to,localpath,end= ) print(as,mimetype,encoding or ‘‘) #保存文件 if mimetype == text and encoding == None: localfile = open(localpath,w,encoding=connection.encoding) callback = lambda line: localfile.write(line + \n) connection.retrlines(RETR +remotename,callback) else: localfile = open(localpath,wb) connection.retrbinary(RETR +remotename,localfile.write) localfile.close() count += 1 connection.quit() print(Done:,count,file download.)

利用FTP服務器上傳目錄

import os,sys
from ftplib import FTP
from mimetypes import guess_type

nonpassive = False                                                         #passive FTP by default
remotesite = 192.168.191.1
remotedir = RRR                                                            #FTP的路徑
remoteuser = ()                                                            #因為我沒設置密碼,所以為空集

localdir = TTT                                                             #本地路徑

clean_all = input( Clean local directory first? )[:1] in [y,Y]       #是否清除遠程目錄所有文件
#連接PFTP
print(connecting...)
connection = FTP(remotesite)
connection.login(*remoteuser)
connection.cwd(remotedir)
if nonpassive:
    connection.set_pasv(False)                                             #most servers do passive
#清除
if clean_all:
    for remotename in connection.nlst():
        try:
            print(deleting local,remotename)
            connection.delete(remotename)
        except:
            print(cannot delete, remotename)

count = 0
localfiles = os.listdir(localdir)
#只能下載目錄中的文件,不能下載目錄中的目錄
for localname in localfiles[:5]:
    mimetype,encoding = guess_type(localname)
    mimetype = mimetype or ?/?
    mimetype = mimetype.split(/)[0]

    localpath = os.path.join(localdir,localname)
    print(downing,localname,to,localpath,end= )
    print(as,mimetype,encoding or ‘‘)
    #保存文件
    if mimetype == text and encoding == None:
        localfile = open(localpath,rb)
        connection.storlines(RETR +localname,localfile)
    else:
        localfile = open(localpath,rb)
        connection.storbinary(RETR +localname,localfile)

    localfile.close()
    count += 1

connection.quit()
print(Done:,count,file uploaded.)

2.4 利用FTP服務器下載和上傳目錄