Python allows you to download files from FTP and distribute them to multiple machines simultaneously via multiple threads

Source: Internet
Author: User
Tags ftp file ssh port

Python is a very powerful programming language, known as the universal Binder in the programming language, it can be a perfect docking with most of the existing programming languages, today to say that using Python to write a file downloaded from FTP, Then through the Python multi-threaded module threading simultaneously distributed to many machines, even hundreds of machines, many do not say, directly on the code, the code will explain in detail the operation of each step.

According to your actual situation to modify the script to achieve multi-threaded remote seamless operation of the server.

#!/usr/bin/python#coding: utf-8# load the modules we need to use from ftplib import ftpimport  tarfileimport sysimport threadingimport paramikoimport osimport datetimeimport  The time#paramiko  module needs to download the Exitflag = 0reload (SYS) sys.setdefaultencoding ("GBK") according to the different versions on the Internet. Connect FTP function Def ftpconnect (host,username,password):   ftp = ftp ()               #实例化FTP模块   ftp.set_debuglevel (2)    #打开调试模式. Level 2, Show Details   ftp.connect (host,21)     #连接ftp服务器   ftp.login (Username,password)   #登录FTP服务器     return ftp   #返回状态 # Download file Def downlocadfile (FTP, Remotepath,localpath):  bufsize = 1024   #设置缓冲块大小   fp = open ( LocalPath, ' WB ')    #以写的模式打开本地文件   ftp.retrbinary (' retr  '  + remotepath,fp.write , bufsize)   #接收服务器上文件并写入本地文件   ftp.set_debuglevel (0)   #关闭调试   fp.close ()    #关闭文件 # Upload file Def uploadfile (FTP, Remotepath,localpath):  bufsize = 1024    #设置缓冲块大小   fp =  open (localpath,  ' RB ')    #以只读的模式在本地打开文件   ftp.storbinary (' stor  '  +  remotepath, fp, bufsize)    #上传文件   ftp.set_debuglevel (0)    #关闭调试模式    fp.close ()    #关闭文件 # Multithreading simultaneously sends classclass mythread  (threading) operation requests to multiple machines. Thread):    #必须继承父类threading. Thread,    def __init__ (self, hostname, filename):     # Self represents the class itself, and hostname and filename are the parameter         threading of the pass. Thread.__init__ (self)          #每次调用这个class的时候, both of these parameters are initialized          self.hostname = hostname                 #设置成其他函数也可以调用的属性          Self.filename = filename    def run (self):                     #把要执行的代码写到run函数里面   The thread runs the run function directly after it is created          username= '                 #机器的账号          password= '     #机器的密码         port=                    # The SSH port of the machine         local_file= '/home/mf/%s '  % self.filename      #本地文件         remote_file= '/home/mf/%s '  %  self.filename    #你想要将文件传到对What path         t=paramiko the square machine. Transport ((self.hostname,port))         #类实例化          t.connect (Username=username,password=password)     #远程连接服务器          sftp=paramiko. Sftpclient.from_transport (t)         #使用 (t) to connect the host     #         files=sftp.listdir (Dir_path)           #files =os.listdir (local_dir)           #for   f in files:         print  '          print  ' ######################################### '          print  ' beginning to upload file %s  '  %  Datetime.datetime.now ()     #打印连接状态及时间          #print   ' uploading file: ', Os.path.join (local_dir,f)                print  ' uploading file: ',local_file                # sftp.get (Os.path.join (dir_path,f), Os.path.join (local_path,f))           sftp.put (Local_file,remote_file)              print  ' upload file success %s  '  %  Datetime.datetime.now ()         print  '          print  ' ########################################## ' #  create a new thread if __name__  ==  "__main__":   try:    remotefile = raw_input (' Enter the ftp file you want to download: '). Strip ()      #输入你要下载的文件名称     localfile = str (raw_input (' input file names stored locally are the best and FTP files with the same name: '). Strip ())     #输入你要存在本地后的文件名     remote_file = remotefile.decode (' UTF8 ', ' ignore ')      #设置文件字符编码     ftp = ftpconnect (' ftp_ip ', ' Ftp_user ', ' ftp_pwd ')       #连接FTP     downlocadfile (Ftp,remote_file, "/home/mf/%s"  % localfile)     #下载文件到本地     ftp.quit ()      #关闭FTP       #现在将文件使用多线程的方式传输到多台服务器甚至上百上千台服务器上. Selects = raw_input (' Input transfer file: ')     #输入你要传输的文件名称      #ip_list  =  []    #ip地址表 # Here you can use the loop to achieve, because my time is not enough, no time to think about it, the great God wanted to come out, please tell me thank you. Thread1 = mythread (ip[0], selects)   #  Create new Thread Thread1.start ()   #  Turn on thread   except exception, e:  print e

Python to download files from FTP and distribute them to multiple machines simultaneously through multi-threaded

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.