Aug. 7, 2007, 1:33 p.m.
Easy HTTP Parsing With Python
I'm writing some code to read a pcap file to produce input for a load tester. This is simple HTTP over TCP and I'm picking out the layers in the packet structure as they go. I'm using simple struct stuff to get through IP and TCP, but when I got to HTTP, I wanted to see if there was something easier to do for understanding HTTP.
This is the simultaneously ugly and beautiful parser I ended up with:
class HTTPDecoder(BaseHTTPServer.BaseHTTPRequestHandler): def __init__(self, v): self.rfile=StringIO.StringIO(v) self.raw_requestline=self.rfile.readline() self.parse_request() self.request_data=self.rfile.read()
Perhaps it's bad abstraction on their part, but parse_request() is exactly the functionality I wanted.