Example analysis of file download for Python network programming

Source: Internet
Author: User
Tags datetime ftp ftp client ftp protocol in python

This article illustrates the implementation of file download for Python network programming. Share to everyone for your reference. Specifically as follows:

It's so easy to understand the HTTP and FTP downloads that it offers, because it's more and more like python.

1, the corresponding module

The Ftplib module defines the FTP class and some methods for client-side FTP programming. We can write a self FTP client program in python to download a file or mirror site. For more information about the FTP protocol, refer to RFC959 or see Python help.

Urllib module provides a very advanced interface to crawl data from the network, the main use of the Urlopen function is similar to the function of the open function, here we want to use the Urlretrieve () function to download files from the HTTP server.

2, the instance realizes the FTP downloading and uploads

?

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) #打开调试级别2, displaying more information Ftp.connec T (' **ip** ') #连接ftp服务器 ftp.login (user,password) #输入用户名和密码 print ftp.getwelcome () #显示ftp服务器的欢迎信息 ftp.cwd (path) #选择操作目录 BufSize = 1024 #设置缓冲区大小 file_handler = open (file, ' WB '). Write #以写模式在本地打开文件 strbuffer = ' RETR ' + file Ftp.retrbinary (strbuf fer,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 On the path and file name, except Ioerror:print "Please input the correct path and filename" Else:ftpdownload (path1,file1)

Upload file is very similar to the corresponding upload function storbinary.

?

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** ') f Tp.login (User,password) print ftp.getwelcome () ftp.cwd (path) bufsize = 1024 File_handler = open (file, ' RB ') #读方式打开上传文件 str Buffer = ' 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, the implementation of HTTP download instance

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

?

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 '% Startti Me urllib.urlretrieve (URL, ' test.exe ') #开始下载, test.exe file name saved after download endtime = Datetime.datetime.now () print ' Download end Time is%s '% endtime print ' To download the file use time%s s '% (endtime-starttime). seconds if __name__ = ' __main__ ' : If Len (sys.argv) = = 2:try:download (sys.argv[1]) except Ioerror:print ' url not found ' else:download (' Http://www.pytho n.org/')

I hope this article will help you with your Python programming.

Related Article

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.