Python ';非类型';对象没有属性';集团';列表中

Python ';非类型';对象没有属性';集团';列表中,python,python-2.7,match,Python,Python 2.7,Match,多亏了这段代码,我正试图将一组数据放入我的列表中,但info_散列有一些问题: handshake = piece_request_handshake.findall(hex_data) match = piece_request_handshake.match(hex_data) info_hash = match.group('info_hash') # If the packet is a packet type h

多亏了这段代码,我正试图将一组数据放入我的列表中,但info_散列有一些问题:

handshake = piece_request_handshake.findall(hex_data)
            match = piece_request_handshake.match(hex_data)
            info_hash = match.group('info_hash')

            # If the packet is a packet type handshake, if the dest and src ip are not in the list "liste" and if the handshake is not empty, then we add the adress src and dest to the list
            if handshake and (src_ip+" "+dst_ip+" "+info_hash) not in liste and (dst_ip+" "+src_ip+" "+info_hash) not in liste and handshake != '':
                liste.append(src_ip+" "+dst_ip+" "+info_hash)
但我不知道为什么它会返回这个错误:

root@debian:/home/florian/Documents/mysite/polls# python scriptbdd.py
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
    self.run()
  File "scriptbdd.py", line 134, in run
    self.p.dispatch(0, PieceRequestSniffer.cb)
  File "scriptbdd.py", line 80, in cb
    info_hash = match.group('info_hash')
AttributeError: 'NoneType' object has no attribute 'group'

我真的不明白在多次尝试后我如何解决它,我请求您的帮助。

这是因为您的正则表达式引擎没有找到匹配项,所以它返回
,而您试图获得它的
。要了解这一点,您可以使用
尝试,但
除外:

handshake = piece_request_handshake.findall(hex_data)
            match = piece_request_handshake.match(hex_data)
            try :
              info_hash = match.group('info_hash')
              # If the packet is a packet type handshake, if the dest and src ip are not in the list "liste" and if the handshake is not empty, then we add the adress src and dest to the list
              if info_hash and handshake and (src_ip+" "+dst_ip+" "+info_hash) not in liste and (dst_ip+" "+src_ip+" "+info_hash) not in liste and handshake != '':
                liste.append(src_ip+" "+dst_ip+" "+info_hash)
            except AttributeError:
                 print 'there is no match'

这是因为您的正则表达式引擎没有找到匹配项,所以它返回
None
,您试图获取它的
组。要了解这一点,您可以使用
尝试,但
除外:

handshake = piece_request_handshake.findall(hex_data)
            match = piece_request_handshake.match(hex_data)
            try :
              info_hash = match.group('info_hash')
              # If the packet is a packet type handshake, if the dest and src ip are not in the list "liste" and if the handshake is not empty, then we add the adress src and dest to the list
              if info_hash and handshake and (src_ip+" "+dst_ip+" "+info_hash) not in liste and (dst_ip+" "+src_ip+" "+info_hash) not in liste and handshake != '':
                liste.append(src_ip+" "+dst_ip+" "+info_hash)
            except AttributeError:
                 print 'there is no match'

这是因为您的正则表达式引擎没有找到匹配项,所以它返回
None
,您试图获取它的
组。要了解这一点,您可以使用
尝试,但
除外:

handshake = piece_request_handshake.findall(hex_data)
            match = piece_request_handshake.match(hex_data)
            try :
              info_hash = match.group('info_hash')
              # If the packet is a packet type handshake, if the dest and src ip are not in the list "liste" and if the handshake is not empty, then we add the adress src and dest to the list
              if info_hash and handshake and (src_ip+" "+dst_ip+" "+info_hash) not in liste and (dst_ip+" "+src_ip+" "+info_hash) not in liste and handshake != '':
                liste.append(src_ip+" "+dst_ip+" "+info_hash)
            except AttributeError:
                 print 'there is no match'

这是因为您的正则表达式引擎没有找到匹配项,所以它返回
None
,您试图获取它的
组。要了解这一点,您可以使用
尝试,但
除外:

handshake = piece_request_handshake.findall(hex_data)
            match = piece_request_handshake.match(hex_data)
            try :
              info_hash = match.group('info_hash')
              # If the packet is a packet type handshake, if the dest and src ip are not in the list "liste" and if the handshake is not empty, then we add the adress src and dest to the list
              if info_hash and handshake and (src_ip+" "+dst_ip+" "+info_hash) not in liste and (dst_ip+" "+src_ip+" "+info_hash) not in liste and handshake != '':
                liste.append(src_ip+" "+dst_ip+" "+info_hash)
            except AttributeError:
                 print 'there is no match'
re.match()
如果表达式与字符串不匹配,并且
None
没有属性
group
,则返回
None
。在对
re.match()
执行任何操作之前,您必须检查它的返回:

match = piece_request_handshake.match(hex_data)
if match is not None:
    info_hash = match.group('info_hash')
    # etc
else:
    # do whatever else
    print "didn't match"
re.match()
如果表达式与字符串不匹配,并且
None
没有属性
group
,则返回
None
。在对
re.match()
执行任何操作之前,您必须检查它的返回:

match = piece_request_handshake.match(hex_data)
if match is not None:
    info_hash = match.group('info_hash')
    # etc
else:
    # do whatever else
    print "didn't match"
re.match()
如果表达式与字符串不匹配,并且
None
没有属性
group
,则返回
None
。在对
re.match()
执行任何操作之前,您必须检查它的返回:

match = piece_request_handshake.match(hex_data)
if match is not None:
    info_hash = match.group('info_hash')
    # etc
else:
    # do whatever else
    print "didn't match"
re.match()
如果表达式与字符串不匹配,并且
None
没有属性
group
,则返回
None
。在对
re.match()
执行任何操作之前,您必须检查它的返回:

match = piece_request_handshake.match(hex_data)
if match is not None:
    info_hash = match.group('info_hash')
    # etc
else:
    # do whatever else
    print "didn't match"

piece\u请求握手。match(十六进制数据)
正在返回
None
piece\u请求握手。match(十六进制数据)
正在返回
None
piece\u请求握手。match(十六进制数据)
正在返回
None
piece\u请求握手。match(十六进制数据)
正在返回
None
谢谢,对不起,我的级别,现在它引发了这个错误:
线程1中的异常:回溯(最近一次调用):文件“/usr/lib/python2.7/threading.py”,第552行,在uu bootstrap_internal self.run()文件“scriptbdd.py”中,第137行,在运行self.p.dispatch(0,PieceRequestSniffer.cb)文件“scriptbdd.py”中,第117行,如果元素==(src_ip2+“”+dst_ip2+“”+info_hash)或element==(dst_ip2+“”+src_ip2+“”+info_hash):UnboundLocalError:赋值前引用的局部变量“info_hash”
@Kastra:好主意,我不明白为什么问题仍然存在:
线程1中的异常:回溯(最后一次调用):文件“/usr/lib/python2.7/threading.py”,第552行,在文件“scriptbdd.py”中,第137行,在运行self.p.dispatch(0,PieceRequestSniffer.cb)文件“scriptbdd.py”中,第86行,在cb中,如果信息散列和握手,(src_ip+“”+dst_ip+“”+info_散列)不在列表中,并且(dst_ip+“”+src_ip+“”+info_散列)不在列表和握手中!='':UnboundLocalError:分配前引用的局部变量“info_hash”
@Bouh10 Ah您需要将其余代码放入
try
中。检查编辑谢谢,抱歉我的级别,现在它引发此错误:
线程中的异常-1:回溯(最后一次调用):文件”/usr/lib/python2.7/threading.py”,第552行,在内部self.run()文件“scriptbdd.py”中,第137行,在运行self.p.dispatch(0,PieceRequestSniffer.cb)文件“scriptbdd.py”中,第117行,在cb中,如果元素==(src_ip2+“”+dst_ip2+“”+info_hash)或元素=(dst_ip2+“”+src_ip2+“”+info_hash):UnboundLocalError:赋值前引用的局部变量“info_hash”
@Kastra:好主意,我不明白为什么问题仍然存在:
线程线程中的异常-1:回溯(最近一次调用):文件“/usr/lib/python2.7/threading.py”,第552行,在u bootstrap\u internal self.run()文件“scriptbdd.py”中,第137行,在运行self.p.dispatch(0,PieceRequestSniffer.cb)文件“scriptbdd.py”中,第86行,如果信息散列和握手,则在cb中,并且(src_ip+“”+dst_ip+“”+info_散列)不在列表中,并且(dst_ip+“”+src_ip+“”+info_散列)不在列表和握手中!='':UnboundLocalError:分配前引用的局部变量“info_hash”
@Bouh10 Ah您需要将其余代码放入
try
中。检查编辑谢谢,抱歉我的级别,现在它引发此错误:
线程1中的异常:回溯(最后一次调用):文件“/usr/lib/python2.7/threading.py”,第552行,在文件“scriptbdd.py”中,第137行,在运行self.p.dispatch(0,PieceRequestSniffer.cb)文件“scriptbdd.py”中,第117行,在cb中,如果元素==(src_ip2+“”+dst_ip2+“”+info_hash)或元素==(dst_ip2+“”+src_ip2+“”+info_hash):UnboundLocalError:赋值前引用的局部变量“info_hash”
@Kastra:好主意,我不明白为什么问题仍然存在:
线程线程中的异常-1:回溯(最近一次调用):文件“/usr/lib/python2.7/threading.py”,第552行,在u bootstrap\u internal self.run()文件“scriptbdd.py”中,第137行,在运行self.p.dispatch(0,PieceRequestSniffer.cb)文件“scriptbdd.py”中,第86行,在cb中如果信息散列和握手,并且(src_ip+“”+dst_ip+“”+info_散列)不在列表中,并且(dst_ip+“”+src_ip+“”+info_散列)不在列表和握手中!='':UnboundLocalError:localvariable'info_散列