python实现的简单FTP上传下载文件实例
作者:lover007 时间:2022-12-29 13:17:42
本文实例讲述了python实现的简单FTP上传下载文件的方法。分享给大家供大家参考。具体如下:
python本身自带一个FTP模块,可以实现上传下载的函数功能。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from ftplib import FTP
def ftp_up(filename = "20120904.rar"):
ftp=FTP()
ftp.set_debuglevel(2)
#打开调试级别2,显示详细信息;0为关闭调试信息
ftp.connect('192.168.0.1','21')
#连接
ftp.login('admin','admin')
#登录,如果匿名登录则用空串代替即可
#print ftp.getwelcome()
#显示ftp服务器欢迎信息
#ftp.cwd('xxx/xxx/')
#选择操作目录
bufsize = 1024
#设置缓冲块大小
file_handler = open(filename,'rb')
#以读模式在本地打开文件
ftp.storbinary('STOR %s' % os.path.basename(filename),file_handler,bufsize)
#上传文件
ftp.set_debuglevel(0)
file_handler.close()
ftp.quit()
print "ftp up OK"
def ftp_down(filename = "20120904.rar"):
ftp=FTP()
ftp.set_debuglevel(2)
ftp.connect('192.168.0.1','21')
ftp.login('admin','admin')
#print ftp.getwelcome()
#显示ftp服务器欢迎信息
#ftp.cwd('xxx/xxx/')
#选择操作目录
bufsize = 1024
filename = "20120904.rar"
file_handler = open(filename,'wb').write
#以写模式在本地打开文件
ftp.retrbinary('RETR %s' % os.path.basename(filename),file_handler,bufsize)
#接收服务器上文件并写入本地文件
ftp.set_debuglevel(0)
file_handler.close()
ftp.quit()
print "ftp down OK"
希望本文所述对大家的Python程序设计有所帮助。
标签:python,FTP,上传,下载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python进程的通信Queue、Pipe实例分析
2021-11-22 13:50:44
![](https://img.aspxhome.com/file/2023/8/65078_0s.png)
python3爬虫获取html内容及各属性值的方法
2021-08-23 02:37:33
windows安装python超详细图文教程
2023-08-01 15:19:32
![](https://img.aspxhome.com/file/2023/2/61762_0s.png)
实例讲解Python脚本成为Windows中运行的exe文件
2023-07-15 02:14:31
python调用并链接MATLAB脚本详解
2021-12-11 07:16:42
详解python3百度指数抓取实例
2022-01-11 03:06:59
![](https://img.aspxhome.com/file/2023/2/102802_0s.png)
python爬虫beautiful soup的使用方式
2023-05-17 08:13:22
![](https://img.aspxhome.com/file/2023/7/72267_0s.png)
Oracle性能究极优化 下
2010-07-30 13:25:00
Python 如何展开嵌套的序列
2022-10-12 03:15:37
如何优化JavaScript脚本的性能
2008-02-19 21:47:00
python openCV实现摄像头获取人脸图片
2021-08-16 08:21:03
![](https://img.aspxhome.com/file/2023/5/68475_0s.jpg)
python去除扩展名的实例讲解
2022-05-08 18:10:49
Python实现从订阅源下载图片的方法
2021-08-14 19:19:37
Python学习笔记之抓取某只基金历史净值数据实战案例
2021-08-14 20:28:13
![](https://img.aspxhome.com/file/2023/2/97142_0s.png)
Safari参考样式库之webkit
2009-07-26 09:50:00
![](https://img.aspxhome.com/file/UploadPic/20097/26/webkit1-13s.jpg)
python多线程超详细详解
2023-08-09 09:10:23
SQL Server数据库超级管理员账号防护
2008-12-22 16:30:00
python用opencv批量截取图像指定区域的方法
2021-10-14 09:07:33
python实现猜数字游戏(无重复数字)示例分享
2023-12-15 19:48:46
php根据isbn书号查询amazon网站上的图书信息的示例
2023-11-15 03:08:53