Python network programming-analysis of file download instances

Source: Internet
Author: User
Tags ftp file ftp client ftp protocol
This article describes how to download python files through network programming. The example shows how to download Python files through FTP and http, for more information about how to download python files, see the example in this article. Share it with you for your reference. The details are as follows:

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 FTPimport sysdef ftpdownload (path, file): ftp = FTP () ftp. set_debuglevel (2) # enable debug level 2 and display the ftp details. connect ('** IP **') # connect to the ftp server. 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 strBuffer = 'retr '+ file ftp locally in write mode. retrbinary (strBuffer, file_handler, bufsize) # receives files from the server and writes them to the local file ftp. set_debuglevel (0) # Disable ftp debugging. 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]) # path and file name of the command line input file on ftp, handle T IOError: print "please input the correct path and filename" else: ftpdownload (path1, file1)

The upload function storbinary is similar.

From ftplib import FTPimport sys, osdef 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 an ftp file. 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 urllibimport sysdef download (url): starttime = datetime. datetime. now () print 'download start time is % s' % starttime urllib.urlretrieve(url,'test.exe ') start download, test.exe is the name of the file saved after download endtime = datetime. datetime. now () print 'download end time is % s' % endtime print 'you download the file use time % s' % (endtime-starttime ). secondsif _ 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 /')

I hope this article will help you with Python programming.

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.