Example analysis of file download of Python network programming

Source: Internet
Author: User
This paper describes the implementation method of file download for Python network programming. Share to everyone for your reference. Specific as follows:

The more you look like Python, the easier it is to understand the HTTP and FTP downloads it offers.

1. Corresponding module

The Ftplib module defines the FTP class and some methods for client FTP programming. We can use Python to write a self-made FTP client program for downloading files or mirror sites. If you want to know more about the FTP protocol, please refer to RFC959 or see Python help.

The Urllib module provides a very advanced interface to fetch data from the network, mainly using the Urlopen function, similar to the function of the open function, where we use the Urlretrieve () function to download files from the HTTP server.

2. FTP download and upload for instance

From ftplib import ftpimport sysdef ftpdownload (path,file):  FTP = FTP ()  ftp.set_debuglevel (2)  #打开调试级别2, Show Details  ftp.connect (' **ip** ')  #连接ftp服务器  ftp.login (user,password)  #输入用户名和密码  Print Ftp.getwelcome ()  #显示ftp服务器的欢迎信息  ftp.cwd (path)  #选择操作目录  bufsize = 1024x768  #设置缓冲区大小  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)

Uploading the file is very similar, the corresponding upload function storbinary.

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 = 1024x768  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 download For instance

HTTP download is really super simple, a function is done, here by passing in to download the address to download the file, and calculate the download time, I think the feeling is relatively stupid method of calculating time, do not know who has the clever idea?

Import urllibimport sysdef Download (URL):  starttime = Datetime.datetime.now ()  print ' Download start time is%s '% StartTime  urllib.urlretrieve (URL, ' test.exe ')  #开始下载, Test.exe is the file name saved after download  endtime = Datetime.datetime.now ()  print ' Download end time is%s '% endtime    print ' You download the file use time%s s '% (en dtime-starttime). secondsif __name__ = = ' __main__ ':  If Len (sys.argv) = = 2:    try:      download (sys.argv[1])    except IOError:      print ' url not found '  else:    download (' http://www.python.org/')

Hopefully 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.