Python daily progress (8)-network programming File Download

Source: Internet
Author: User

The more you see it, the more you like python. If you want to know the http and ftp download functions provided by python, it turns out to be so simple.

1. corresponding modules

The ftplib module defines FTP classes and some methods for ftp programming on the client. We can use python to compile an ftp client program for downloading files or image sites. For details about the ftp protocol, refer to RFC959 or view python help.

The Urllib module provides a very advanced interface to capture data from the network. The urlopen function is mainly used, which is similar to the open function. urlretrieve () is used here () function to download files from the http server.


2. Implement FTP download and upload for instances

 

From ftplib import FTP
Import sys
Def ftpdownload (path, file ):
Ftp = FTP ()
Ftp. set_debuglevel (2) # enable debug level 2 and display details
Ftp. connect ('** IP **') # connect to the ftp server
Ftp. login (user, password) # enter the user name and password
Print ftp. getwelcome () # display ftp server welcome information
Ftp. cwd (path) # select the operation directory
Bufsize = 1024 # Set the buffer size
File_handler = open (file, 'wb '). write # open the file locally in write mode
StrBuffer = 'retr '+ file
Ftp. retrbinary (strBuffer, file_handler, bufsize) # receives files from the server and writes them to a local file
Ftp. set_debuglevel (0) # disable debugging
Ftp. quit () # exit the ftp server
If _ name _ = '_ main __':
Path1 = 'Download/test /'
File1 = 'test1.rar'
If len (sys. argv) = 3:
Try:
Ftpdownload (sys. argv [1], sys. argv [2]) # enter the ftp path and file name of the file in the command line,
Handle t IOError:
Print "please input the correct path and filename"
Else:
Ftpdownload (path1, file1)

 

The upload function storbinary is similar.

 

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') # open the uploaded file in Read mode
StrBuffer = 'retr '+ file
Ftp. storbinary (strBuffer, file_handler, bufsize) # upload a file
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])
Handle t IOError:
Print "please input the correct path and filename"
Else:
Ftpdownload (path1, file1)

 

3. Implement HTTP download for instances

Http download is really super simple, just get it done with a function. Here, we pass in the address to download the file and calculate the download time. I think it is a stupid way to calculate the time, I don't know who has a high skill?

 

Import urllib
Import sys
Def download (url ):
Starttime = datetime. datetime. now ()
Print 'Download start time is % s' % starttime
Urllib.urlretrieve(url,'test.exe ') starts and downloads. test.exe is the name of the downloaded file.
Endtime = datetime. datetime. now ()
Print 'Download end time is % s' % endtime
Print you download the file use time % s '% (endtime-starttime). seconds

If _ name _ = '_ main __':
If len (sys. argv) = 2:
Try:
Download (sys. argv [1])
Handle t IOError:
Print 'url not found'
Else:
Download ('HTTP: // www.python.org /'')

 

Vivilisa write in 03.20.2009

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.