From 967e6cdb7ab1d123795e5d54fc810e8b6a4cf471 Mon Sep 17 00:00:00 2001 From: mark mcintyre Date: Tue, 2 Apr 2019 15:10:09 -0400 Subject: [PATCH] adding a simple web server for stack testing --- README.md | 3 ++- python-web-server | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100755 python-web-server diff --git a/README.md b/README.md index c4f2267..55383c7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ # misc-tools -various tools for miscellaneous use \ No newline at end of file +Various tools for miscellaneous use. + diff --git a/python-web-server b/python-web-server new file mode 100755 index 0000000..644430f --- /dev/null +++ b/python-web-server @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 + +import argparse +import http.server +import socketserver +import logging + +# setting up logging for this script +_level = logging.INFO +_format = "%(asctime)-15s [%(levelname)-8s] %(lineno)d : %(funcName)s : %(message)s" +logging.basicConfig(format=_format, level=_level) +log = logging.getLogger() + + +class SimpleHandler(http.server.BaseHTTPRequestHandler): + + def do_GET(self): + log.info(f"server version: {self.server_version}") + log.info(f"client address: {self.client_address}") + self.send_response(200) + self.send_header('Content-type', 'text/html') + self.end_headers() + self.wfile.write(b"

Hello, World!

") + return + +class SockHandler(socketserver.BaseRequestHandler): + + def handle(self): + self.data = self.request.recv(1024).strip() + log.info(f"client address: {self.client_address}") + log.info(f"received data:\n{self.data}") + self.request.sendall(b"Hello, World!\n") + + +def parse_args(): + """ + Parse args + """ + argp = argparse.ArgumentParser() + argp.add_argument('-i', '--ip-address', default="", help="IP Address to bind the server") + argp.add_argument('-p', '--port', type=int, default=8080, help="Port on which to listen for traffic") + argp.add_argument('-d', '--debug', help="Run in debug mode") + return argp.parse_args() + + +if __name__ == "__main__": + args = parse_args() + + if args.debug: + log.setLevel(logging.DEBUG) + + try: + #simple_handler = SimpleHandler() # http.server.SimpleHTTPRequestHandler + + with http.server.HTTPServer((args.ip_address, args.port), SimpleHandler) as httpd: + log.info(f"Server started on {args.ip_address}:{args.port}") + httpd.serve_forever() + + #with socketserver.TCPServer((args.ip_address, args.port), SockHandler) as tcpd: + # log.info(f"Server started on {args.ip_address}:{args.port}") + # tcpd.serve_forever() + + except KeyboardInterrupt as control_c: + log.info("Server stopped") +