简体   繁体   中英

Regex on array of chars in python?

I have a buffer (an array of chars) that I am using to read data in from a socket, which contains an HTTP request. I have some regular expressions that work nicely for extracting relevant info from strings, and I am looking for a way to use those regular expressions to extract the same info from an array instead, without having to build a string out of the array. Is this possible with ctypes? This is an example of how I am getting the data right now.

import socket, array, ctypes
libc = ctypes.cdll.LoadLibrary('libc.so.6')
buff = array.array('c', '\0'*4096)
a, b = socket.socketpair()
fd = a.fileno()
buff_pointer = buff.buffer_info()[0]
b.send('a'*100)
bytes_read = libc.recv(fd, buff_pointer, len(buff), 0)
print buff #prints a zeroed array of length 4096 with 100 chars of 'a' in front

This is purely for fun/for lulz btw, inb4 it's unpythonic.

Just run your regexs on the array object, eg

>>> import re
>>> m = re.match('^aaaaa', buff)
>>> m
<_sre.SRE_Match object at 0x7fd4cd2cd030>
>>> m.group()
array('c', 'aaaaa')
>>> m.string[m.start():m.end()]
array('c', 'aaaaa')

etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM