python re compile无法获取_python – 如何在re.compile之后获取一个元素?

使用“re”我编译握手的数据,如下所示:

piece_request_handshake = re.compile('13426974546f7272656e742070726f746f636f6c(?P\w{16})(?P\w{40})(?P\w{40})')

handshake = piece_request_handshake.findall(hex_data)

然后我打印出来

我无法添加图片,因为我是新手,所以这是输出:

root@debian:/home/florian/Téléchargements# python script.py

[('0000000000100005', '606d4759c464c8fd0d4a5d8fc7a223ed70d31d7b', '2d5452323532302d746d6e6a657a307a6d687932')]

我的问题是,我怎么才能把这个数据的第二部分称为“hash_info”(“606d47 ……”)?

我已经尝试使用以下行的re组:

print handshake.group('info_hash')

但结果是错误(再次抱歉,我无法显示屏幕……):

*root@debian:/home/florian/Téléchargements# python script.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 "script.py", line 122, in run

self.p.dispatch(0, PieceRequestSniffer.cb)

File "script.py", line 82, in cb

print handshake.group('info_hash')

AttributeError: 'list' object has no attribute 'group'*

这是我对好奇的完整代码的开始:

import pcapy

import dpkt

from threading import Thread

import re

import binascii

import socket

import time

liste=[]

prefix = '13426974546f7272656e742070726f746f636f6c'

hash_code = re.compile('%s(?P\w{16})(?P\w{40})(?P\w{40})' % prefix)

match = hash_code.match()

piece_request_handshake = re.compile('13426974546f7272656e742070726f746f636f6c(?P\w{16})(?P\w{40})(?P\w{40})')

piece_request_tcpclose = re.compile('(?P\w{12})5011')

#-----------------------------------------------------------------INIT------------------------------------------------------------

class PieceRequestSniffer(Thread):

def __init__(self, dev='eth0'):

Thread.__init__(self)

self.expr = 'udp or tcp'

self.maxlen = 65535 # max size of packet to capture

self.promiscuous = 1 # promiscuous mode?

self.read_timeout = 100 # in milliseconds

self.max_pkts = -1 # number of packets to capture; -1 => no limit

self.active = True

self.p = pcapy.open_live(dev, self.maxlen, self.promiscuous, self.read_timeout)

self.p.setfilter(self.expr)

@staticmethod

def cb(hdr, data):

eth = dpkt.ethernet.Ethernet(str(data))

ip = eth.data

#------------------------------------------------------IPV4 AND TCP PACKETS ONLY---------------------------------------------------

#Select Ipv4 packets because of problem with the .p in Ipv6

if eth.type == dpkt.ethernet.ETH_TYPE_IP6:

return

else:

#Select only TCP protocols

if ip.p == dpkt.ip.IP_PROTO_TCP:

tcp = ip.data

src_ip = socket.inet_ntoa(ip.src)

dst_ip = socket.inet_ntoa(ip.dst)

fin_flag = ( tcp.flags & dpkt.tcp.TH_FIN ) != 0

#if fin_flag:

#print "TH_FIN src:%s dst:%s" % (src_ip,dst_ip)

try:

#Return hexadecimal representation

hex_data = binascii.hexlify(tcp.data)

except:

return

#-----------------------------------------------------------HANDSHAKE-------------------------------------------------------------

handshake = piece_request_handshake.findall(hex_data)

if handshake and (src_ip+" "+dst_ip) not in liste and (dst_ip+" "+src_ip) not in liste and handshake != '':

liste.append(src_ip+" "+dst_ip)

print match.group('info_hash')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值