欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)ftp客戶端示例分享

 更新時間:2014年02月17日 10:11:18   作者:  
這篇文章主要介紹了python實現(xiàn)ftp客戶端示例,包括ftp的常見任務(wù),上傳,下載,刪除,更名等功能,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

#!/usr/bin/python
#coding:utf-8
#write:JACK
#info:ftp example
import ftplib, socket, os
from time import sleep, ctime

def LoginFtp(self):
        ftps = ftplib.FTP()
        ftps.connect(self.host,self.port)
        ftps.login(self.name,self.passwd)

#未進行判斷地址輸入是否為ip或者域名;可以進行判斷是否包含<或者實體符號以及';其他可以忽略
class LoFtp(object):
    'this is ftp class example'
    host = str(raw_input('host,127.0.0.1\n'))
    if host == '':host = '127.0.0.1'

    port = raw_input('port,21\n')
    if not(port.isdigit()):port =21

    name = str(raw_input('name,anonymous\n'))
    if name=='':name='anonymous'

    passwd = str(raw_input('password\n'))
    if passwd =='':passwd=''

    def ZqFtp(self,host,name,passwd,port):
        self.host = host
        self.name = name
        self.passwd = passwd
        self.port = port

    def LoginFtp(self):
        self.ftps = ftplib.FTP()
        self.ftps.connect(self.host,self.port)
        self.ftps.login(self.name,self.passwd)
        self.buffer = 2048 #設(shè)置緩存大小

    def ShowFtp(self):
        self.LoginFtp()
        self.ftps.dir('/')
        dirs = str(raw_input('PLEASE INPUT DIR!\n'))
        print self.ftps.dir(dirs)


    def UpFtp(self):
        'uploads files'
        self.LoginFtp()
        self.ftps.set_debuglevel(2)
        filename = str(raw_input('PLEASE FILE NAME!\n'))
        file_open=open(filename,'rb') #打開文件 可讀即可
        self.ftps.storbinary('STOR %s'% os.path.basename(filename),file_open,self.buffer)
        # 上傳文件
        self.ftps.set_debuglevel(0)
        file_open.close()

    def DelFtp(self):
        'Delete Files'
        self.LoginFtp()
        filename=str(raw_input('PLEASE DELETE FILE NAME!\n'))
        self.ftps.delete(filename)

    def RemoveFtp(self):
        'Remove File'
        self.LoginFtp()
        self.ftps.set_debuglevel(2)#調(diào)試級別,0無任何信息提示
        oldfile=str(raw_input('PLEASE OLD FILE NAME!\n'))
        newfile=str(raw_input('PLEASE NEW FILE NAME!\n'))
        self.ftps.rename(oldfile,newfile)
        self.ftps.set_debuglevel(0)

    def DownFtp(self):
        'Download File'
        self.LoginFtp()
        self.ftps.set_debuglevel(2)
        filename=str(raw_input('PLEASE FILE NAME!\n'))
        file_down = open(filename,'wb').write
        self.ftps.retrbinary('STOP %s' % os.path.basename(filename),file_down,self.buffer)
        self.ftps.set_debuglevel(0)
        file_down.close()

 

a = LoFtp()
print a.ShowFtp()

while True:
    helpn= str(raw_input('Whether to continue to view or exit immediately!(y/n/q)\n'))
    if (helpn=='y')or(helpn=='Y'):
        dirs = str(raw_input('PLEASE INPUT DIR!\n'))
        a.ftps.dir(dirs)
    elif (helpn=='q')or (helpn=='Q'):
        exit()
    else:
        break

 

while True:
    print '上傳請選擇----1'
    print '下載請選擇----2'
    print '修改FTP文件名稱----3'
    num = int(raw_input('PLEASE INPUT NUMBER![exit:5]\n'))

    if num ==1:
        upf = a.UpFtp()
        print 'Upfile ok!'
    elif num ==2:
        dof = a.DownFtp()
        print 'Download file ok!'
    elif num ==3:
        ref = a.RemoveFtp()
        print 'Remove file ok!'
    else:
        a.ftps.quit()
        print 'Bingo!'
        break


#login(user='anonymous',passwd='', acct='') 登錄到FTP服務(wù)器,所有的參數(shù)都是可選的
#pwd()                                     得到當(dāng)前工作目錄
#cwd(path)                                 把當(dāng)前工作目錄設(shè)置為path
#dir([path[,...[,cb]])       顯示path目錄里的內(nèi)容,可選的參數(shù)cb 是一個回調(diào)函數(shù),它會被傳給retrlines()方法
#nlst([path[,...])           與dir()類似,但返回一個文件名的列表,而不是顯示這些文件名
#retrlines(cmd [, cb])       給定FTP 命令(如“RETR filename”),用于下載文本文件??蛇x的回調(diào)函數(shù)cb 用于處理文件的每一行
#retrbinary(cmd, cb[,bs=8192[, ra]])        與retrlines()類似,只是這個指令處理二進制文件?;卣{(diào)函數(shù)cb 用于處理每一塊(塊大小默認(rèn)為8K)下載的數(shù)據(jù)。
#storlines(cmd, f)           給定FTP 命令(如“STOR filename”),以上傳文本文件。要給定一個文件對象f
#storbinary(cmd, f[,bs=8192])               與storlines()類似,只是這個指令處理二進制文件。要給定一個文件對象f,上傳塊大小bs 默認(rèn)為8Kbs=8192])
#rename(old, new)            把遠(yuǎn)程文件old 改名為new
#delete(path)                刪除位于path 的遠(yuǎn)程文件
#mkd(directory)              創(chuàng)建遠(yuǎn)程目錄
#每個需要輸入的地方,需要進行排查檢錯。僅僅這個功能太小了。不過根據(jù)實際情況更改,放在bt里邊當(dāng)個小工具即可
#有點爛,沒有做任何try

相關(guān)文章

最新評論