Created
August 29, 2016 05:56
-
-
Save kamchy/06a86d5d38be88325d073970325e618e to your computer and use it in GitHub Desktop.
Simple python http server that logs data from post requests to file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from sys import * | |
import os | |
from http.server import * | |
class LogLineHandler (BaseHTTPRequestHandler): | |
def do_GET(s): | |
print("Handling get request .") | |
def do_POST(s): | |
s.send_response(200) | |
s.end_headers() | |
varLen = int(s.headers['Content-Length']) | |
postVars = s.rfile.read(varLen).decode("UTF-8") | |
if not s.server.lpf.closed: | |
s.server.lpf.write(postVars + "\n") | |
s.server.lpf.flush() | |
else: | |
print(postVars) | |
def run(log_path, server_class=HTTPServer, handler_class=LogLineHandler): | |
server_address = ('', 8080) | |
httpd = server_class(server_address, handler_class) | |
httpd.log_path = log_path | |
with open(log_path, "w+") as httpd.lpf: | |
httpd.serve_forever() | |
if __name__ == "__main__": | |
if len(sys.argv) > 1 and not os.path.exists(sys.argv[1]): | |
log_path = sys.argv[1] | |
print("starting logger server - logs post request data to %s" % log_path) | |
run(log_path) | |
else: | |
print("log path not give, exiting...") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment