python网络编程之文件下载实例分析

yipeiwu_com6年前Python基础

本文实例讲述了python网络编程之文件下载实现方法。分享给大家供大家参考。具体如下:

真是越看越喜欢python啊,想要了解它提供的http和ftp下载功能,原来是如此的简单。

1、相应模块

ftplib模块定义了FTP类和一些方法,用以进行客户端的ftp编程。我们可用python编写一个自已的ftp客户端程序,用于下载文件或镜像站点。如果想了解ftp协议的详细内容,请参考RFC959或是查看python帮助吧。

Urllib模块提供了非常高级的接口来从网络上抓取数据,主要使用到的是urlopen函数,跟open函数功能比较相似,这里我们要用到urlretrieve()函数来实现从http服务器上下载文件。

2、实例实现FTP下载和上传

from ftplib import FTP
import sys
def ftpdownload(path,file):
  ftp = FTP()
  ftp.set_debuglevel(2)
  #打开调试级别2,显示详细信息
  ftp.connect('**IP**')
  #连接ftp服务器
  ftp.login(user,password)
  #输入用户名和密码
  print ftp.getwelcome()
  #显示ftp服务器的欢迎信息
  ftp.cwd(path)
  #选择操作目录
  bufsize = 1024
  #设置缓冲区大小
  file_handler = open(file,'wb').write
  #以写模式在本地打开文件
  strBuffer = 'RETR ' + file
  ftp.retrbinary(strBuffer,file_handler,bufsize)
  #接收服务器上文件并写入本地文件
  ftp.set_debuglevel(0) #关闭调试
  ftp.quit() #退出ftp服务器
if __name__ == '__main__':
  path1 = 'download/test/'
  file1 = 'test1.rar'
  if len(sys.argv) == 3:
    try:
      ftpdownload(sys.argv[1],sys.argv[2])
      #命令行输入文件在ftp上的路径和文件名,    
    except IOError:
      print "please input the correct path and filename"
  else:
    ftpdownload(path1,file1)

上传文件非常类似,对应的上传函数storbinary。

from ftplib import FTP
import sys,os
def ftpdownload(path,file):
  ftp = FTP()
  ftp.set_debuglevel(2)
  ftp.connect('**IP**')
  ftp.login(user,password)
  print ftp.getwelcome()
  ftp.cwd(path)
  bufsize = 1024
  file_handler = open(file,'rb')
  #读方式打开上传文件
  strBuffer = 'RETR ' + file
  ftp.storbinary(strBuffer,file_handler,bufsize)
  #上传文件
  ftp.set_debuglevel(0) 
  ftp.quit()
if __name__ == '__main__':
  path1 = 'download/test/'
  file1 = '4.jpg'
  if len(sys.argv) == 3:
    try:
      ftpdownload(sys.argv[1],sys.argv[2])      
    except IOError:
      print "please input the correct path and filename"
  else:
    ftpdownload(path1,file1)

3、实例实现HTTP下载

http下载真的是超级简单,一个函数就搞定,这里通过传入要下载的地址下载文件,并计算下载时间,我想的感觉是比较笨的计算时间的方法,不知道谁有高招呢?

import urllib
import sys
def download(url):
  starttime = datetime.datetime.now()
  print 'download start time is %s'% starttime
  urllib.urlretrieve(url,'test.exe')
  #开始下载,test.exe为下载后保存的文件名
  endtime = datetime.datetime.now()
  print 'download end time is %s'% endtime  
  print 'you download the file use time %s s' % (endtime - starttime).seconds
if __name__ == '__main__':
  if len(sys.argv) == 2:
    try:
      download(sys.argv[1])
    except IOError:
      print 'url not found'
  else:
    download('http://www.python.org/')

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

相关文章

Python+PIL实现支付宝AR红包

Python+PIL实现支付宝AR红包

本文实例为大家分享了Python+PIL处理支付宝AR红包的具体代码,供大家参考,具体内容如下 思路比较简单: 1、对图片进行锐化处理; 2、设(r_h, g_h, b_h)为支付宝遮...

python读取excel指定列数据并写入到新的excel方法

如下所示: #encoding=utf-8 import xlrd from xlwt import * #------------------读数据----------------...

Python使用 Beanstalkd 做异步任务处理的方法

Python使用 Beanstalkd 做异步任务处理的方法

使用 Beanstalkd 作为消息队列服务,然后结合 Python 的装饰器语法实现一个简单的异步任务处理工具. 最终效果 定义任务: from xxxxx.job_queue i...

快速实现基于Python的微信聊天机器人示例代码

快速实现基于Python的微信聊天机器人示例代码

最近听说一个很好玩的图灵机器人api,正好可以用它做一个微信聊天机器人,下面是实现 # test.py import requests import itchat #这是一个用于...

python实现的登录和操作开心网脚本分享

SNS什么的我是一直无爱的,这次蛋疼写了个登录开心网(kaixin001)并向所有好友发送站内消息的脚本。 开心网在登录的时候做了一些处理,并不传原始密码,从js分析到的结果是:登录时会...