python实现简单ftp客户端的方法

作者:不吃皮蛋 时间:2023-12-14 20:07:31 

本文实例讲述了python实现简单ftp客户端的方法。分享给大家供大家参考。具体实现方法如下:


#!/usr/bin/python
# -*- coding: utf-8 -*-
import ftplib
import os
import socket
HOST = 'ftp.mozilla.org'
DIRN = 'pub/mozilla.org/webtools'
FILE = 'bugzilla-3.6.9-to-3.6.10-nodocs.diff.gz'
def writedata(data):
 f = open(FILE,'wb')
 try:
   f.write(data)
 finally:
   f.close()
def main():
 try:
   f = ftplib.FTP(HOST)
 except (socket.error, socket.gaierror):
   print 'ERROR:cannot reach " %s"' % HOST
   return
 print '***Connected to host "%s"' % HOST
 try:
   f.login()
 except ftplib.error_perm:
   print 'ERROR: cannot login anonymously'
   f.quit()
   return
 print '*** Logged in as "anonymously"'
 try:
   f.cwd(DIRN)
 except ftplib.error_perm:
   print 'ERRORL cannot CD to "%s"' % DIRN
   f.quit()
   return
 print '*** Changed to "%s" folder' % DIRN
 try:
   #传一个回调函数给retrbinary() 它在每接收一个二进制数据时都会被调用
   f.retrbinary('RETR %s' %FILE, writedata)
 except ftplib.error_perm:
   print 'ERROR: cannot read file "%s"' %FILE
   os.unlink(FILE)
 else:
   print '*** Downloaded "%s" to CWD' % FILE
 f.quit()
 return
if __name__ == '__main__':
 main()

希望本文所述对大家的Python程序设计有所帮助。

标签:python,ftp
0
投稿

猜你喜欢

  • SQL Server 触发器 表的特定字段更新时,触发Update触发器

    2024-01-25 09:32:20
  • python pprint模块中print()和pprint()两者的区别

    2023-10-18 07:34:18
  • Javascript实现购物车功能的详细代码

    2024-05-22 10:32:27
  • 浅谈django url请求与数据库连接池的共享问题

    2024-01-26 00:12:40
  • Random 在 Python 中的使用方法

    2022-04-29 14:21:37
  • asp 动态生成rss(不成生xml文件)代码

    2011-04-04 11:17:00
  • Win10下Python3.7.3安装教程图解

    2023-09-02 18:16:54
  • python使用matplotlib模块绘制多条折线图、散点图

    2021-07-28 06:41:20
  • Golang利用channel协调协程的方法详解

    2024-05-08 10:21:54
  • python项目报错:bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requests

    2022-07-17 05:39:02
  • SQL游标原理和使用方法

    2008-12-22 10:50:00
  • call在Python中改进数列的实例讲解

    2021-10-12 17:17:20
  • JDBC利用C3P0数据库连接池连接数据库

    2024-01-13 12:37:51
  • python3+PyQt5 自定义窗口部件--使用窗口部件样式表的方法

    2023-04-03 04:19:06
  • Oracle 处理json数据的方法

    2024-01-16 15:11:15
  • [翻译]标记语言和样式手册 Chapter 8 再谈清单

    2008-01-29 13:16:00
  • 如何编写高质量的Javascript代码

    2011-03-07 16:04:00
  • ORACLE隐藏参数查看及修改的方法

    2024-01-13 02:33:27
  • Python数据正态性检验实现过程

    2022-07-10 15:46:14
  • Python自定义一个类实现字典dict功能的方法

    2023-07-20 22:45:59
  • asp之家 网络编程 m.aspxhome.com