2022-06-07 13:49:14 +05:30
|
|
|
"""
|
2022-02-01 23:01:57 +05:30
|
|
|
Copyright 2022 The Rook Authors. All rights reserved.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
2025-03-03 18:34:27 +05:30
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2022-02-01 23:01:57 +05:30
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
2022-06-07 13:49:14 +05:30
|
|
|
"""
|
2024-01-30 20:01:18 +05:30
|
|
|
|
2022-02-01 23:01:57 +05:30
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""
|
|
|
|
|
Very simple HTTP server in python for logging requests
|
|
|
|
|
Usage::
|
|
|
|
|
./server.py [<port>]
|
|
|
|
|
"""
|
|
|
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
|
|
|
import logging
|
|
|
|
|
|
2022-06-07 13:49:14 +05:30
|
|
|
|
2022-02-01 23:01:57 +05:30
|
|
|
class S(BaseHTTPRequestHandler):
|
|
|
|
|
def _set_response(self):
|
|
|
|
|
self.send_response(200)
|
2022-06-07 13:49:14 +05:30
|
|
|
self.send_header("Content-type", "text/html")
|
2022-02-01 23:01:57 +05:30
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
|
|
def do_POST(self):
|
2022-06-07 13:49:14 +05:30
|
|
|
content_length = int(
|
|
|
|
|
self.headers["Content-Length"]
|
|
|
|
|
) # <--- Gets the size of data
|
|
|
|
|
post_data = self.rfile.read(content_length) # <--- Gets the data itself
|
|
|
|
|
logging.info("POST request\nBody:\n%s\n", post_data.decode("utf-8"))
|
|
|
|
|
|
2022-02-01 23:01:57 +05:30
|
|
|
|
|
|
|
|
def run(server_class=HTTPServer, handler_class=S, port=8080):
|
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
2022-06-07 13:49:14 +05:30
|
|
|
server_address = ("", port)
|
2022-02-01 23:01:57 +05:30
|
|
|
httpd = server_class(server_address, handler_class)
|
2022-06-07 13:49:14 +05:30
|
|
|
logging.info("Starting httpd...\n")
|
2022-02-01 23:01:57 +05:30
|
|
|
try:
|
|
|
|
|
httpd.serve_forever()
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
pass
|
|
|
|
|
httpd.server_close()
|
2022-06-07 13:49:14 +05:30
|
|
|
logging.info("Stopping httpd...\n")
|
2022-02-01 23:01:57 +05:30
|
|
|
|
2022-06-07 13:49:14 +05:30
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-02-01 23:01:57 +05:30
|
|
|
from sys import argv
|
|
|
|
|
|
|
|
|
|
if len(argv) == 2:
|
|
|
|
|
run(port=int(argv[1]))
|
|
|
|
|
else:
|
|
|
|
|
run()
|