首页 > 编程 > Python > 正文

python实现ftp客户端示例分享

2020-02-23 05:11:35
字体:
来源:转载
供稿:网友

代码如下:
#!/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 #设置缓存大小

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表