The example in this paper is about the simple FTP upload and download file implemented by Python. Share to everyone for your reference. Specific as follows:
Python itself comes with an FTP module, which enables the function of uploading and downloading functions.
#!/usr/bin/env python #-*-coding:utf-8-*-from ftplib import FTP def ftp_up (filename = "20120904.rar"): Ftp=ftp () Ftp.set_debuglevel (2) #打开调试级别2, show details, 0 for off debug information ftp.connect (' 192.168.0.1 ', ' $ ') #连接 ftp.login (' admin ', ' admin ') #登录, such as The anonymous login is replaced with an empty string #print ftp.getwelcome () #显示ftp服务器欢迎信息 #ftp. CWD (' xxx/xxx/') #选择操作目录 bufsize = 1024x768 #设置缓冲块大小 file_h Andler = open (filename, ' RB ') #以读模式在本地打开文件 ftp.storbinary (' STOR%s '% os.path.basename (filename), file_handler,bufsize) #上传文件 ftp.set_debuglevel (0) File_handler.close () ftp.quit () print "ftp up OK" def ftp_down (filename = "20120904.R Ar "): Ftp=ftp () ftp.set_debuglevel (2) ftp.connect (' 192.168.0.1 ', ' + ') ftp.login (' admin ', ' admin ') #print FTP.GETW elcome () #显示ftp服务器欢迎信息 #ftp. CWD (' xxx/xxx/') #选择操作目录 bufsize = 1024x768 filename = "20120904.rar" File_handler = Open (FileName, ' WB '). Write #以写模式在本地打开文件 ftp.retrbinary (' RETR%s '% os.path.basename (filename), file_handler,bufsize) # Receive files on the server and write to local files Ftp.set_debugLevel (0) File_handler.close () ftp.quit () print "FTP Down OK"
Hopefully this article will help you with Python programming.