# -*- coding: utf-8 -*- import ftplib import os class XMyFtp:time_out = 30 ftp_user = "yxjk" ftp_pwd = "yxjk" ftp_host = "192.168.10.95" ftp_port = 21 ftp = None #FTP初始化 def __init__(self):self.ftp = ftplib.FTP()try:self.ftp.connect(self.ftp_host,self.ftp_port)except:print "无法连接远程服务器" exit(-1)try:self.ftp.login(self.ftp_user,self.ftp_pwd)except:print "用户名、密码不正确" exit(-1)self.ftp.set_pasv(False)print self.ftp.getwelcome() #获取FTP服务器默认路径下的目录列表 def GetRemoteRootDir(self):pwdPath = self.ftp.pwd() #获取FTP当前路径 if pwdPath == '/' or pwdPath == './':return self.ftp.nlst()else:self.ftp.cwd('./')return self.ftp.nlst()#上传文件到FTP服务器 #注意:file_path不可为目录,必须为带有文件名的路径 def UploadFile(self,local_path,ftp_path):if(os.path.exists(local_path)):try:self.ftp.storbinary('STOR %s'%ftp_path, open(local_path,'rb'))print "文件%s上传成功" % local_pathexcept:print "文件%s上传失败" % local_pathelse:print "目标文件不存在" #上传目录到FTP服务器 def UploadDir(self,local_dir,ftp_dir):upload_dir = None self.ftp.cwd('/')if(os.path.isabs(local_dir)):dir,file = os.path.split(local_dir)upload_dir = fileelse:upload_dir = local_dirtry:self.ftp.cwd(ftp_dir)except:try:self.ftp.mkd(ftp_dir) #首次创建目录会有异常,但目录却会创建成功,在捕获异常之后尝试切换目录 except:self.ftp.cwd(ftp_dir)remote_dir = ftp_dir.lstrip('/');if(self.ftp.pwd().lstrip('/') == remote_dir):remote_dir = remote_dir.split('/')remote_dir = remote_dir[len(remote_dir)-1]self.ftp.cwd('/')if(remote_dir == upload_dir):for file in os.listdir(local_dir):if os.path.isdir(file):self.UploadDir(local_dir+'/'+file,ftp_dir)else:self.UploadFile(local_dir+'/'+file,ftp_dir+'/'+file)else:self.UploadDir(local_dir,ftp_dir+'/'+remote_dir)else:print "远程目录非法" #print self.ftp.pwd() self.ftp.close() #myftp = XMyFtp() #myftp.GetRemoteDir() #myftp.UploadFile('e:/pydev/Eobject/upload/d.txt','inc/upload/d.txt') #myftp.UploadDir("e:/pydev/Eobject/upload",'inc/upload')
详细解决方案
自己写的python操作FTP服务器(未完,没写下载操作)
热度:26 发布时间:2024-01-19 08:10:37.0
相关解决方案
- ubuntu9.04上安装mic2 出错 (python setup.py build)
- ubuntu9.04下安装mic2 出错 (python setup.py build),该如何解决
- python sybase装配
- python(django框架) + mysql 的编码有关问题,求解
- SAE python web.py bootstrap 开发记要
- 数目字代码把大数据数字口语化(python+js) 数字代码
- python JSON string 的中文有关问题
- python――施用yaml数据格式,PK -> XML,JSON
- Learn Python The Hard Way学习(52) - 开始您的web游戏
- Python 相关有关问题
- 菜鸟,先学JAVA,还是Ruby,python
- 【大哥大姐】python 操作MS SQL Server 2005 效率
- python serial模块运用
- python scp自动登录解决方法
- 【Python】按行读取文件、IOError: [Errno 22] invalid mode ('a+') or filename,处置文件的换行符
- Python 3.x中使用print函数出现语法异常(SyntaxError: invalid syntax)的原因
- 【Error】Python安装gevent时出错:“致命异常: Python.h:没有那个文件或目录”
- [Python]attributeError:'module' object has no attribute 'dump'
- Python 出错:IndentationError: unexpected indent
- Python“TypeError: 'NoneType' object is not iterable"的缘故
- python in操作挑动 TypeError
- 【Error】Python:SyntaxError: Non-ASCII character '\xe5'解决办法
- python 有什么 步骤可以知道函数或类在哪个文件定义的
- Python 将数组转换为 Wav
- Python:文件格式不正确
- 如何在 Bash 的 python 解释器中运行 python 脚本
- python-改进脚本和异常处理
- Python(附加列表时出现内存错误)
- Python:另存为字符串时,保存数字错误
- Python:如何编写代码序列?