-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_server.py
79 lines (63 loc) · 2.86 KB
/
simple_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
"""
Utility to create a simple HTTP(s) server for testing purpose.
SSL certificate: openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
from sys import argv
import requests
import ssl
import logging
import argparse
forward = None
class SimpleServer(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
self._set_response()
self.wfile.write("GET request for {}".format(self.path).encode('utf-8'))
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
str(self.path), str(self.headers), post_data.decode('utf-8'))
if forward:
forward_request(forward,post_data)
self._set_response()
self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))
def forward_request(forward, data):
try:
response = requests.post(forward, data, timeout=0.0000000001)
except requests.exceptions.ReadTimeout:
pass
def run(port, connection, keyfile, certfile, server_class=HTTPServer, handler_class=SimpleServer):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
if connection == 'https':
httpd.socket = ssl.wrap_socket (httpd.socket, keyfile=keyfile, certfile=certfile, server_side=True)
logging.info('httpd Server Started...\n')
try:
httpd.serve_forever()
except KeyboardInterrupt:
logging.info('Exception: unable to start httpd server...\n')
httpd.server_close()
logging.info('Stopping httpd...\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='''Utility to create a simple HTTP(s) server that log incoming requests for testing purposes.
For now, it supports GET and POST methods.''',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-t", "--type", choices=["http","https"], type=str, help="create a server with http or https capabilities",
default="http")
parser.add_argument("-p", "--port", type=int, help="define server port", default=4444)
parser.add_argument("-k", "--key", help="path to keyfile (required for SSL)", default='./key.pem')
parser.add_argument("-c", "--cert", help="path to certificate file (required for SSL)", default='./cert.pem')
parser.add_argument("-f", "--forward", help="forward incoming POST request toward a defined endpoint. \
Define endpoint following the format => proto:ip:port (e.g., http://127.0.0.1:8000/ )", default=None)
args = parser.parse_args()
forward = args.forward
run(port=args.port,connection=args.type, keyfile=args.key, certfile=args.cert)