2011-05-03 37 views
-2

我试图在与已经工作的本地函数若干不同路径(Python 2.7版登录到Ubuntu的服务器和搜索日志调用一个函数 - WIN7机)。下面是我如何登录,并选择日志的功能(也,我的方案的基础是Python的CMD模块):我想了解如何从另一个功能相同的类

def do_siteserver(self, line): 
     import paramiko 
     paramiko.util.log_to_file('c:\Python27\paramiko-wininst.log') 
     host = '10.5.48.65' 
     portNum = raw_input("\nEnter a port number: ") 
     while True: 
      try: 
       port = int(portNum) 
       break 
      except: 
       print "\nPort is not valid!!!" 
       break     
     transport = paramiko.Transport((host,port)) 
     while True: 
      try: 
       passW = raw_input("\nEnter the SiteServer weekly password: ") 
       password = passW 
       username = 'gilbert' 
       nport = str(port) 
       print '\nEstablishing SFTP connection to: {}:{} ...'.format(host,port) 
       transport.connect(username = username, password = password) 
       sftp = paramiko.SFTPClient.from_transport(transport) 
       print 'Authorization Successful!!!' 
       log_names = ("/var/log/apache2/access.log", 
          "/var/log/apache2/error.log", 
          "/var/opt/smartmerch/log/merch_error.log", 
          "/var/opt/smartmerch/log/merch_event.log", 
          "/var/opt/smartmerch/log/server_sync.log") 

       #call search function here? 

       #for log_file, local_name in log_names.iteritems(): 
       #  sftp.get(log_file, local_name) 
       #sftp.close() 
       #transport.close() 
       break 
      except: 
       print "\nAuthorization Failed!!!" 

下面是函数(在同一个班),我想打电话:

def do_search(self, line): 
    print '\nCurrent dir: '+ os.getcwd() 
    userpath = raw_input("\nPlease enter a path to search (only enter folder name, eg. SQA\log): ") 
    directory = os.path.join("c:\\",userpath) 
    os.chdir(directory) 
    print "\n        SEARCHES ARE CASE SENSITIVE" 
    print " " 
    line = "[1]Single File [2]Multiple Files [3]STATIC HEX" 
    col1 = line[0:14] 
    col2 = line[15:32] 
    col3 = line[33:46] 
    print "     " + col1 + "  " + col2 + "  " + col3 
    print "\nCurrent Dir: " + os.getcwd() 
    searchType = raw_input("\nSelect type of search: ") 
     if searchType == '1': 
      logfile = raw_input("\nEnter filename to search (eg. name.log): ") 
      fiLe = open(logfile, "r") 
      userString = raw_input("\nEnter a string name to search: ") 
      for i,line in enumerate(fiLe.readlines()): 
       if userString in line: 
       print "String: " + userString 
       print "File: " + os.join(directory,logfile) 
       print "Line: " + str(i) 
       break 
      else: 
       print "%s NOT in %s" % (userString, logfile) 

      fiLe.close() 
     elif searchType =='2': 
      print "\nDirectory to be searched: " + directory 
      #directory = os.path.join("c:\\","SQA_log") 
      userstring = raw_input("Enter a string name to search: ") 
      userStrHEX = userstring.encode('hex') 
      userStrASCII = ''.join(str(ord(char)) for char in userstring) 
      regex = re.compile(r"(%s|%s|%s)" % (re.escape(userstring), re.escape(userStrHEX), re.escape(userStrASCII))) 
      choice = raw_input("1: search with respect to whitespace. 2: search ignoring whitespace: ") 
      if choice == '1': 
       for root,dirname, files in os.walk(directory): 
        for file in files: 
         if file.endswith(".log") or file.endswith(".txt"): 
         f=open(os.path.join(root, file)) 
         for i,line in enumerate(f.readlines()): 
          result = regex.search(line) 
          if result: 
           print "\nLine: " + str(i) 
           print "File: " + os.path.join(root,file) 
           print "String Type: " + result.group() + '\n' 



         f.close() 
      re.purge()    
      if choice == '2': 
      regex2 = re.compile(r'\s+') 
      for root,dirname, files in os.walk(directory): 
       for file in files: 
        if file.endswith(".log") or file.endswith(".txt"): 
         f=open(os.path.join(root, file)) 
         for i,line in enumerate(f.readlines()): 
          result2 = regex.search(re.sub(regex2, '',line)) 
          if result2: 
           print "\nLine: " + str(i) 
           print "File: " + os.path.join(root,file) 
           print "String Type: " + result2.group() + '\n' 


         f.close() 


         re.purge() 

     elif searchType =='3': 
      print "\nDirectory to be searched: " + directory 
      print " " 
      #directory = os.path.join("c:\\","SQA_log") 
      regex = re.compile(r'(?:3\d){6}') 
      for root,dirname, files in os.walk(directory): 
      for file in files: 
       if file.endswith(".log") or file.endswith(".txt"): 
        f=open(os.path.join(root,file)) 
        for i, line in enumerate(f.readlines()): 
         searchedstr = regex.findall(line) 
         ln = str(i) 
         for word in searchedstr: 
         print "\nString found: " + word 
         print "Line: " + ln 
         print "File: " + os.path.join(root,file) 
         print " " 
         logfile = open('result3.log', 'w') 

        f.close() 

      re.purge() 

回答

2
self.do_search(linenumber) 

这是所有。

2

的方法是通过有他们的对象调用。

self.do_search(...) 
相关问题