Python network programming-Analysis of 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
?
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) # Enable debug level 2 to display details Ftp. connect ('** IP **') # Connecting 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) # Selecting the operation directory Bufsize = 1024 # Set the buffer size File_handler = open (file, 'wb '). write # Open a file locally in write mode StrBuffer = 'retr '+ file Ftp. retrbinary (strBuffer, file_handler, bufsize) # Receiving files from the server and writing 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]) # Command line input file path and file name on ftp, Handle t IOError: Print "please input the correct path and filename" Else: Ftpdownload (path1, file1) |
The upload function storbinary is similar.
?
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 ') # Open an uploaded file in Read mode StrBuffer = 'retr '+ file Ftp. storbinary (strBuffer, file_handler, bufsize) # Uploading files 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?
?
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 ') Download starts and starts. test.exe is the downloaded file name. 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 /') |
I hope this article will help you with Python programming.