Przeglądaj źródła

若出现ftp相关函数连接超时,调用ftp.set_pasv(True)即可。

JeffWang 3 lat temu
rodzic
commit
4476a5affb
1 zmienionych plików z 84 dodań i 63 usunięć
  1. 84 63
      ftp.py

+ 84 - 63
ftp.py

@@ -4,14 +4,17 @@ import sys
 import os
 import socket
 from ftplib import FTP_TLS
-import re # 正则表达式;
+#from ftplib import FTP
+import re  # 正则表达式;
 import shutil
 
+
 class MyFTP:
     def __init__(self, host, port=21):
         self.host = host
         self.port = port
         self.ftp = FTP_TLS()
+        #self.ftp = FTP()
         self.ftp.encoding = 'utf-8'
         self.log_file = open("log.txt", "a")
         self.file_list = []
@@ -23,35 +26,35 @@ class MyFTP:
             timeout = 60
             socket.setdefaulttimeout(timeout)
             # 0主动模式 1 #被动模式
-            self.ftp.set_pasv(False)
+            self.ftp.set_pasv(True)
             # 打开调试级别2,显示详细信息
             # self.ftp.set_debuglevel(2)
 
-            self.debug_print('开始尝试连接到 %s' % self.host)
+            self.debug_print(u'try to connect %s' % self.host)
             self.ftp.connect(self.host, self.port)
-            self.debug_print('成功连接到 %s' % self.host)
+            self.debug_print(u'connect success %s' % self.host)
 
-            self.debug_print('开始尝试登录到 %s' % self.host)
+            self.debug_print(u'try to login %s' % self.host)
             self.ftp.login(username, password)
             self.ftp.prot_p()
-            self.debug_print('成功登录到 %s' % self.host)
+            self.debug_print(u'login success %s' % self.host)
 
             self.debug_print(self.ftp.welcome)
         except Exception as err:
-            self.deal_error("FTP 连接或登录失败 ,错误描述为:%s" % err)
+            self.deal_error(u"FTP connect or login faild , error desc = %s" % err)
             pass
 
     def is_same_size(self, local_file, remote_file):
         try:
             remote_file_size = self.ftp.size(remote_file)
         except Exception as err:
-            # self.debug_print("is_same_size() 错误描述为:%s" % err)
+            self.debug_print("is_same_size() error descript:%s" % err)
             remote_file_size = -1
 
         try:
             local_file_size = os.path.getsize(local_file)
         except Exception as err:
-            # self.debug_print("is_same_size() 错误描述为:%s" % err)
+            self.debug_print("is_same_size() error descript:%s" % err)
             local_file_size = -1
 
         self.debug_print('local_file_size:%d  , remote_file_size:%d' % (local_file_size, remote_file_size))
@@ -70,17 +73,17 @@ class MyFTP:
         self.debug_print("download_file()---> local_path = %s ,remote_path = %s" % (local_file, remote_file))
 
         if self.is_same_size(local_file, remote_file):
-            self.debug_print('%s 文件大小相同,无需下载' % local_file)
+            self.debug_print(u'%s same file size, no need to download' % local_file)
             return
         else:
             try:
-                self.debug_print('>>>>>>>>>>>>下载文件 %s ... ...' % local_file)
+                self.debug_print(u'>>>>>>>>>>>>download file %s ... ...' % local_file)
                 buf_size = 1024
                 file_handler = open(local_file, 'wb')
                 self.ftp.retrbinary('RETR %s' % remote_file, file_handler.write, buf_size)
                 file_handler.close()
             except Exception as err:
-                self.debug_print('下载文件出错,出现异常:%s ' % err)
+                self.debug_print(u'error downloading file with exception = %s ' % err)
                 return
 
     def download_file_tree(self, local_path, remote_path):
@@ -94,33 +97,34 @@ class MyFTP:
         try:
             self.ftp.cwd(remote_path)
         except Exception as err:
-            self.debug_print('远程目录%s不存在,继续...' % remote_path + " ,具体错误描述为:%s" % err)
+            self.debug_print(
+                u'remote directory %s does not exist, continue...' % remote_path + u" ,the specific error description is %s" % err)
             return
 
         if not os.path.isdir(local_path):
-            self.debug_print('本地目录%s不存在,先创建本地目录' % local_path)
+            self.debug_print(u'local directory %s does not exsit, create local directory first' % local_path)
             os.makedirs(local_path)
 
-        self.debug_print('切换至目录: %s' % self.ftp.pwd())
+        self.debug_print(u'switch to directory %s' % self.ftp.pwd())
 
         self.file_list = []
         # 方法回调
         self.ftp.dir(self.get_file_list)
 
         remote_names = self.file_list
-        self.debug_print('远程目录 列表: %s' % remote_names)
+        self.debug_print(u'remote directory list: %s' % remote_names)
         for item in remote_names:
             file_type = item[0]
             file_name = item[1]
             local = os.path.join(local_path, file_name)
             if file_type == 'd':
-                print("download_file_tree()---> 下载目录: %s" % file_name)
+                print(u"download_file_tree()---> download directory: %s" % file_name)
                 self.download_file_tree(local, file_name)
             elif file_type == '-':
-                print("download_file()---> 下载文件: %s" % file_name)
+                print(u"download_file()---> download file: %s" % file_name)
                 self.download_file(local, file_name)
             self.ftp.cwd("..")
-            self.debug_print('返回上层目录 %s' % self.ftp.pwd())
+            self.debug_print(u'return to upper level directory %s' % self.ftp.pwd())
         return True
 
     def upload_file(self, local_file, remote_file):
@@ -132,18 +136,18 @@ class MyFTP:
              remote_path: 远程文件
         """
         if not os.path.isfile(local_file):
-            self.debug_print('%s 不存在' % local_file)
+            self.debug_print('%s does not exist' % local_file)
             return
 
         if self.is_same_size(local_file, remote_file):
-            self.debug_print('跳过相等的文件: %s' % local_file)
+            self.debug_print(u'skip equivalent files: %s' % local_file)
             return
 
         buf_size = 1024
         file_handler = open(local_file, 'rb')
         self.ftp.storbinary('STOR %s' % remote_file, file_handler, buf_size)
         file_handler.close()
-        self.debug_print('上传: %s' % local_file + "成功!")
+        self.debug_print('upload: %s' % local_file + "success")
 
     def upload_file_tree(self, local_path, remote_path):
         """从本地上传目录下多个文件到ftp
@@ -154,7 +158,7 @@ class MyFTP:
              remote_path: 远程路径
         """
         if not os.path.isdir(local_path):
-            self.debug_print('本地目录 %s 不存在' % local_path)
+            self.debug_print('local directory %s does not exsit' % local_path)
             return
         """
         创建服务器目录
@@ -163,9 +167,9 @@ class MyFTP:
             self.ftp.cwd(remote_path)  # 切换工作路径
         except Exception as e:
             base_dir, part_path = self.ftp.pwd(), remote_path.split('/')
-            print e,base_dir,part_path
+            print e, base_dir, part_path
             for p in part_path[1:]:
-                print "子目录",p
+                print "Subdirectory", p
                 base_dir = base_dir + p + '/'  # 拼接子目录
                 try:
                     self.ftp.cwd(base_dir)  # 切换到子目录, 不存在则异常
@@ -173,32 +177,32 @@ class MyFTP:
                     print('INFO:', e)
                     self.ftp.mkd(base_dir)  # 不存在创建当前子目录
         self.ftp.cwd(remote_path)
-        self.debug_print('切换至远程目录: %s' % self.ftp.pwd()) 
+        self.debug_print('switch to remote directory: %s' % self.ftp.pwd())
 
         local_name_list = os.listdir(local_path)
-        self.debug_print('本地目录list: %s' % local_name_list)
-        #self.debug_print('判断是否有服务器目录: %s' % os.path.isdir())
+        self.debug_print('local directory list: %s' % local_name_list)
+        # self.debug_print('判断是否有服务器目录: %s' % os.path.isdir())
 
         for local_name in local_name_list:
             src = os.path.join(local_path, local_name)
-            print("src路径=========="+src)
+            print("src directory==========" + src)
             if os.path.isdir(src):
                 try:
                     self.ftp.mkd(local_name)
                 except Exception as err:
-                    self.debug_print("目录已存在 %s ,具体错误描述为:%s" % (local_name, err))
-                self.debug_print("upload_file_tree()---> 上传目录: %s" % local_name)
-                self.debug_print("upload_file_tree()---> 上传src目录: %s" % src)
+                    self.debug_print("folder has exist %s , error descript:%s" % (local_name, err))
+                self.debug_print("upload_file_tree()---> upload dir: %s" % local_name)
+                self.debug_print("upload_file_tree()---> upload src dir: %s" % src)
                 self.upload_file_tree(src, local_name)
             else:
-                self.debug_print("upload_file_tree()---> 上传文件: %s" % local_name)
+                self.debug_print("upload_file_tree()---> upload file: %s" % local_name)
                 self.upload_file(src, local_name)
         self.ftp.cwd("..")
 
     def close(self):
         """ 退出ftp
         """
-        self.debug_print("close()---> FTP退出")
+        self.debug_print("close()---> FTP exit")
         self.ftp.quit()
         self.log_file.close()
 
@@ -212,7 +216,7 @@ class MyFTP:
             参数:
                 e:异常
         """
-        log_str = '发生错误: %s' % e
+        log_str = u'an error occurred : %s' % e
         self.write_log(log_str)
         sys.exit()
 
@@ -267,33 +271,37 @@ class MyFTP:
             if ftpdir != '.' and ftpdir != '..':
                 self.dir_list.append(self.cur_dir + '/' + ftpdir)
         else:
-            print "正规匹配失败",line
+            print "Normal matching failed", line
 
     def get_files(self, path):
         filelist = self.ftp.nlst(path)
         for file in filelist:
-            self.file_list.append(path+'/'+file)
+            self.file_list.append(path + '/' + file)
         return self.file_list
 
     def set_dir(self, path):
-        self.ftp.cwd(path)
+        print("set dir", self.ftp.cwd(path))
 
     def get_dir(self):
         self.cur_dir = self.ftp.pwd()
+        print("get dir", self.cur_dir)
         return self.cur_dir
 
-FTPHOST='10.118.1.85'
-FTPUSER='你的账号'
-FTPPWD='你的密码'
-FTPDIR='/ProjectSoftware/TEST/TV/Regional_Customers/RT2851_MOKA/cts-auth/22Q1/approved'
-LOCALDIR41=r'F:\22Q1-CTS\41'
-LOCALDIR51=r'F:\22Q1-CTS\51'
+
+FTPHOST = '10.118.1.85'
+FTPUSER = 'jianfeng1.wang'
+FTPPWD = 'Moka@233668#'
+FTPDIR41 = u'/ProjectSoftware/TEST/TV/Regional_Customers/RT2841_MOKA/cts-auth/22Q1/approved/'
+FTPDIR51 = u'/ProjectSoftware/TEST/TV/Regional_Customers/RT2851_MOKA/cts-auth/22Q1/approved/'
+LOCALDIR41 = r'F:\22Q1-CTS\41'
+LOCALDIR51 = r'F:\22Q1-CTS\51'
+
 
 # 下载指定ftp路径内的所有的prop文件
 def download_prop(ftp_dir, local_dir):
     ftp = MyFTP(FTPHOST)
     print ftp.login(FTPUSER, FTPPWD)
-    print ftp.set_dir(FTPDIR)
+    print ftp.set_dir(ftp_dir)
     print ftp.get_dirs()
     if os.path.exists(local_dir):
         shutil.rmtree(local_dir)
@@ -304,62 +312,63 @@ def download_prop(ftp_dir, local_dir):
     for path in ftp.dir_list:
         ftp.get_files(path)
 
-    for i in range(ftp.file_list.__len__()-1, -1, -1):
+    for i in range(ftp.file_list.__len__() - 1, -1, -1):
         path = ftp.file_list[i]
-        (filepath,filename) = os.path.split(path)
+        (filepath, filename) = os.path.split(path)
         if filename != 'build.prop':
             ftp.file_list.remove(path)
 
     file_list = []
-    for i in range(0,ftp.file_list.__len__()):
+    for i in range(0, ftp.file_list.__len__()):
         path = ftp.file_list[i]
         (filepath, filename) = os.path.split(path)
-        file_list.append(local_dir+'\\'+str(i)+filename)
-        ftp.download_file(local_dir+'\\'+str(i)+filename, path)
+        file_list.append(local_dir + '\\' + str(i) + filename)
+        ftp.download_file(local_dir + '\\' + str(i) + filename, path)
 
     return file_list
 
+
 # 解析指定的prop文件;
 def parse_prop(path, find_objs):
-    brand=''
+    brand = ''
     prop_list = []
     if os.path.exists(path) is False:
         return
-    
+
     with open(path, 'r') as f:
         content = f.read()
         for obj in find_objs:
-            res=r"\n%s=(.*)\n"%obj
-            print "表达式:",res
+            res = r"\n%s=(.*)\n" % obj
+            print "expression:", res
             p = re.compile(res)
             mo = p.search(content)
             if mo is not None:
                 prop = mo.group()
                 if prop.find("test-keys"):
                     prop = prop.replace("test-keys", "release-keys")
-                prop=prop.strip('\n')
+                prop = prop.strip('\n')
                 prop_list.append(prop)
                 if obj == 'ro.product.brand':
-                    brand=prop.replace(obj+'=','')
+                    brand = prop.replace(obj + '=', '')
 
-    print "prop_list=",prop_list
-    (filepath,filename) = os.path.split(path)
+    print "prop_list=", prop_list
+    (filepath, filename) = os.path.split(path)
     if prop_list.__len__() > 0:
         prop_file = "%s\\%s.prop" % (filepath, brand.lower())
         if os.path.exists(prop_file):
             os.remove(prop_file)
-        print "prop文件:",prop_file
+        print "prop file:", prop_file
         with open(prop_file, 'ab+') as f:
             for prop in prop_list:
-                f.writelines(prop+'\n')
+                f.writelines(prop + '\n')
     # 移除原文件;
     os.remove(path)
-            
 
     return content.split('\n')  # 将待匹配字符串保存在数组中
 
+
 if __name__ == "__main__":
-    file_list = download_prop(FTPDIR, LOCALDIR51)
+    file_list = download_prop(FTPDIR41, LOCALDIR41)
     for file in file_list:
         parse_prop(file, ['ro.product.model',
                           'ro.product.brand',
@@ -372,4 +381,16 @@ if __name__ == "__main__":
                           'ro.build.version.incremental']
                    )
 
-    print "FTP-OK"
+    file_list = download_prop(FTPDIR51, LOCALDIR51)
+    for file in file_list:
+        parse_prop(file, ['ro.product.model',
+                          'ro.product.brand',
+                          'ro.product.name',
+                          'ro.build.flavor',
+                          'ro.build.date',
+                          'ro.build.fingerprint',
+                          'ro.build.date.utc',
+                          'ro.build.description',
+                          'ro.build.version.incremental']
+                   )
+    print "FTP-OK"