最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python網(wǎng)絡(luò)編程之文件下載實(shí)例分析

 更新時間:2015年05月20日 11:02:00   作者:久月  
這篇文章主要介紹了python網(wǎng)絡(luò)編程之文件下載實(shí)現(xiàn)方法,實(shí)例分析了Python基于FTP及http實(shí)現(xiàn)文件下載的技巧,需要的朋友可以參考下

本文實(shí)例講述了python網(wǎng)絡(luò)編程之文件下載實(shí)現(xiàn)方法。分享給大家供大家參考。具體如下:

真是越看越喜歡python啊,想要了解它提供的http和ftp下載功能,原來是如此的簡單。

1、相應(yīng)模塊

ftplib模塊定義了FTP類和一些方法,用以進(jìn)行客戶端的ftp編程。我們可用python編寫一個自已的ftp客戶端程序,用于下載文件或鏡像站點(diǎn)。如果想了解ftp協(xié)議的詳細(xì)內(nèi)容,請參考RFC959或是查看python幫助吧。

Urllib模塊提供了非常高級的接口來從網(wǎng)絡(luò)上抓取數(shù)據(jù),主要使用到的是urlopen函數(shù),跟open函數(shù)功能比較相似,這里我們要用到urlretrieve()函數(shù)來實(shí)現(xiàn)從http服務(wù)器上下載文件。

2、實(shí)例實(shí)現(xiàn)FTP下載和上傳

from ftplib import FTP
import sys
def ftpdownload(path,file):
  ftp = FTP()
  ftp.set_debuglevel(2)
  #打開調(diào)試級別2,顯示詳細(xì)信息
  ftp.connect('**IP**')
  #連接ftp服務(wù)器
  ftp.login(user,password)
  #輸入用戶名和密碼
  print ftp.getwelcome()
  #顯示ftp服務(wù)器的歡迎信息
  ftp.cwd(path)
  #選擇操作目錄
  bufsize = 1024
  #設(shè)置緩沖區(qū)大小
  file_handler = open(file,'wb').write
  #以寫模式在本地打開文件
  strBuffer = 'RETR ' + file
  ftp.retrbinary(strBuffer,file_handler,bufsize)
  #接收服務(wù)器上文件并寫入本地文件
  ftp.set_debuglevel(0) #關(guān)閉調(diào)試
  ftp.quit() #退出ftp服務(wù)器
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)

上傳文件非常類似,對應(yīng)的上傳函數(shù)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、實(shí)例實(shí)現(xiàn)HTTP下載

http下載真的是超級簡單,一個函數(shù)就搞定,這里通過傳入要下載的地址下載文件,并計算下載時間,我想的感覺是比較笨的計算時間的方法,不知道誰有高招呢?

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程序設(shè)計有所幫助。

相關(guān)文章

最新評論

沙河市| 万宁市| 耿马| 丹阳市| 和硕县| 常宁市| 宝山区| 澄城县| 文化| 南投县| 右玉县| 陕西省| 历史| 门头沟区| 福建省| 万山特区| 黑山县| 深州市| 哈密市| 卓资县| 邹城市| 德安县| 宁波市| 保定市| 湾仔区| 呈贡县| 万年县| 云南省| 萍乡市| 嘉义市| 绍兴市| 灵川县| 天水市| 佛冈县| 广宗县| 黔南| 桦甸市| 新昌县| 舞阳县| 淅川县| 淮南市|