是python3的
读取 requests.txt 然后解析输出对象
requests.txt
POST /search.php?test=query HTTP/1.1
Host: testphp.vulnweb.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 27
Origin: http://testphp.vulnweb.com
Connection: close
Referer: http://testphp.vulnweb.com/search.php?test=query
Upgrade-Insecure-Requests: 1
searchFor=asdas&goButton=go
python代码
from __future__ import absolute_import, unicode_literals
from http.server import BaseHTTPRequestHandler
from io import BytesIO
from urllib import parse
class HTTPRequest(BaseHTTPRequestHandler):
def __init__(self, request_text):
self.rfile = BytesIO(request_text)
self.raw_requestline = self.rfile.readline()
self.error_code = self.error_message = None
self.parse_request()
def send_error(self, code, message):
self.error_code = code
self.error_message = message
class Request:
def __init__(self):
self.headers = None
self.params = None
self.data = None
self.path = None
def replace(self, string, payload):
for k, v in self.headers.items():
k.replace(string, payload)
v.replace(string, payload)
for k, v in self.params.items():
self.params[k] = self.params[k].replace(string, payload)
for k, v in self.data.items():
self.data[k] = self.data[k].replace(string, payload)
print(self.data)
class RequestParser(object):
def __init__(self, request_text):
self.request = Request()
try:
self.raw_request = HTTPRequest(request_text)
if self.raw_request.error_code:
raise Exception("failed parsing request")
self.request.method = self.raw_request.command
self.request.path = self.construct_path()
self.request.headers = self.raw_request.headers
self.request.data = self.convert(self.construct_data())
self.request.params = self.convert(self.construct_params())
except Exception as e:
raise e
def convert(self, data):
if isinstance(data, bytes):
return data.decode()
if isinstance(data, (str, int)):
return str(data)
if isinstance(data, dict):
return dict(map(self.convert, data.items()))
if isinstance(data, tuple):
return tuple(map(self.convert, data))
if isinstance(data, list):
return list(map(self.convert, data))
if isinstance(data, set):
return set(map(self.convert, data))
def construct_path(self):
return parse.urlsplit(self.raw_request.path).path
def construct_data(self):
return dict(parse.parse_qsl(self.raw_request.rfile.read(int(self.raw_request.headers.get('content-length')))))
def construct_params(self):
return dict(parse.parse_qsl(parse.urlsplit(self.raw_request.path).query))
with open("requests.txt", "rb") as f:
parser = RequestParser(f.read())
print(parser.request.method) # prints method
print(parser.request.path) # prints request.path
print(parser.request.headers) # prints requests headers
print(parser.request.data) # prints requests body
print(parser.request.params) # prints requests params
输出