Python:属性错误(NoneType对象没有属性拆分)

时间:2014-01-27 18:51:55

标签: python

我想把一些近似值丢弃。

下面给出的函数是抛出属性错误。可能是什么问题?如果可能,请帮助我。

def calculate_tps(filename,identity):
  ##command='tshark -r %s -R -T pdml diameter'
  first_time = None
  last_time=None
  tshark='tshark -r %s -R \'(diameter.Destination-Host==%s)&& (diameter.CC-Request-Type==3)\' -T pdml'% (filename,identity)
  time = None
  req = 0
  for line in os.popen(tshark, 'r'):
    m=pattern.search(line)    
    if m:
      #print m.group(1)
      if m.group(1) == 'timestamp':
    time=m.group(2)
    if not first_time:
      first_time=time
      elif m.group(1) == 'diameter.Session-Id':
        req +=1 
    if(req % 100) ==0:
      print 'processed 1000 requests req number %s ' % req

  # making some approximations discard ms 
  first = long(first_time.split('.').pop(0))
  last =  long(time.split('.').pop(0))
  time_elapsed = first -last
  print "%s requests in %s seconds, tps = %s " % ( req, time_elapsed, req/time_elapsed)   

这是完整的代码。我试图使用tshark计算每秒请求数。

#!/usr/bin/python
import re
import sys
import commands

    import os
    import time


    pattern = re.compile('<field name="(timestamp|diameter.Session-Id)".*value="([^"]+)')


    from optparse import OptionParser
    def parse_options():
      parser = OptionParser()
     # parser.add_option("-f", "--file", dest="filename",
     #                 help="input pcap file", metavar="FILE")
      parser.add_option("-i", "--identity", dest="identity",
                      help="diameter identity to filter on", metavar="IDENTITY")

      opts, args = parser.parse_args()
      return opts, args

    def calculate_tps(filename,identity):
      ##command='tshark -r %s -R -T pdml diameter'
      first_time = None
      last_time=None
      tshark='tshark -r %s -R \'(diameter.Destination-Host==%s)&& (diameter.CC-Request-Type==3)\' -T pdml'% (filename,identity)
      time = None
      req = 0
      for line in os.popen(tshark, 'r'):
        m=pattern.search(line)    
        if m:
          #print m.group(1)
          if m.group(1) == 'timestamp':
        time=m.group(2)
        print 'time is %s' % time
        if not first_time:
          first_time=time
          prin
          elif m.group(1) == 'diameter.Session-Id':
            req +=1 
        if(req % 100) ==0:
          print 'processed 1000 requests req number %s ' % req

      # making some approximations discard ms 
      first = long(first_time.split('.').pop(0))
      last =  long(time.split('.').pop(0))
      time_elapsed = first -last
      print "%s requests in %s seconds, tps = %s " % ( req, time_elapsed,
      req/time_elapsed)       

    def main():
      global options
      options, args = parse_options()
      if len(args) < 1:
        print >>sys.stderr, "missing pcap file.Please specify the pcap"
        sys.exit(1)   
      #print options.identity  



      start_time = time.time()
      calculate_tps(args.pop(0), options.identity) 
      end_time = time.time()
      print 'completed in %s', (end_time - start_time) 

    if __name__ == '__main__':
       main()

2 个答案:

答案 0 :(得分:1)

在此处拨打电话时:

first = long(first_time.split('.').pop(0))
last =  long(time.split('.').pop(0))

你假设每个都是一个字符串。它们似乎在您对文件内容的循环中没有被重新分配,这表明您的匹配没有发生。您可以使用以下内容对条件检查进行包装:

if None in (first_time, time):
  print "Uh oh, one of first_time or time is None - ", first_time, time

这至少会给你一个问题的指标。

答案 1 :(得分:0)

这是因为你的第一个和最后一个不是字符串。所以没有类型的属性拆分。 因此,如果您需要split属性,最好将它们更改为字符串类型。