'NoneType' 对象在列表中没有属性 'group'
'NoneType' object has no attribute 'group' in a list
由于这段代码,我正在尝试将一组数据放入我的列表中,但是 info_hash 有一些问题:
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)
但我不知道为什么 returns 我这个错误:
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'
我真的不明白我怎么解决我试了很多次我请求你的帮助。
这是因为您的正则表达式引擎没有找到匹配项,所以它 returns None
而您试图获取它的 group
。为了获得它,你可以使用 try-except
:
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'
It's quite obvious : re.match()
returns None
如果表达式不匹配字符串,并且 None
没有属性 group
. IOW 你必须在对它做任何事情之前检查 re.match()
的 return:
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"
由于这段代码,我正在尝试将一组数据放入我的列表中,但是 info_hash 有一些问题:
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)
但我不知道为什么 returns 我这个错误:
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'
我真的不明白我怎么解决我试了很多次我请求你的帮助。
这是因为您的正则表达式引擎没有找到匹配项,所以它 returns None
而您试图获取它的 group
。为了获得它,你可以使用 try-except
:
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'
It's quite obvious : re.match()
returns None
如果表达式不匹配字符串,并且 None
没有属性 group
. IOW 你必须在对它做任何事情之前检查 re.match()
的 return:
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"