python網路編程之檔案下載執行個體分析

來源:互聯網
上載者:User

python網路編程之檔案下載執行個體分析

   本文執行個體講述了python網路編程之檔案下載實現方法。分享給大家供大家參考。具體如下:

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

  1、相應模組

  ftplib模組定義了FTP類和一些方法,用以進行用戶端的ftp編程。我們可用python編寫一個自已的ftp用戶端程式,用於下載檔案或鏡像網站。如果想瞭解ftp協議的詳細內容,請參考RFC959或是查看python協助吧。

  Urllib模組提供了非常進階的介面來從網路上抓取資料,主要使用到的是urlopen函數,跟open函數功能比較相似,這裡我們要用到urlretrieve()函數來實現從http伺服器上下載檔案。

  2、執行個體實現FTP下載和上傳

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

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。

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

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下載真的是超級簡單,一個函數就搞定,這裡通過傳入要下載的地址下載檔案,並計算下載時間,我想的感覺是比較笨的計算時間的方法,不知道誰有高招呢?

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.