Analysis of python network programming File Download instances,
This article describes how to download python network programming files. 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.