| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import dummy_killer |
| 5 |
import tornado.ioloop |
| 6 |
import tornado.web |
| 7 |
import tornado.httpserver |
| 8 |
import ssl |
| 9 |
import argparse |
| 10 |
import os |
| 11 |
import sys |
| 12 |
import traceback |
| 13 |
|
| 14 |
class MainHandler(tornado.web.RequestHandler): |
| 15 |
@tornado.gen.coroutine |
| 16 |
def get(self, path): |
| 17 |
if path == '/empty': |
| 18 |
# Return an empty reply |
| 19 |
self.set_header("Content-Type", "text/plain") |
| 20 |
self.write("") |
| 21 |
elif path == '/error_403': |
| 22 |
# Return a 403 HTTP error |
| 23 |
raise tornado.web.HTTPError(403) |
| 24 |
elif path == '/timeout': |
| 25 |
# Wait for 4 seconds before returning an empty reply |
| 26 |
yield tornado.gen.sleep(4) |
| 27 |
self.set_header("Content-Type", "text/plain") |
| 28 |
self.write("") |
| 29 |
elif path == '/request': |
| 30 |
# Return a string 'hello world' |
| 31 |
self.set_header("Content-Type", "text/plain") |
| 32 |
self.write("hello world") |
| 33 |
elif path == '/map-simple': |
| 34 |
# Return a string 'hello map' |
| 35 |
self.set_header("Content-Type", "text/plain") |
| 36 |
self.write("hello map") |
| 37 |
elif path == '/map-query': |
| 38 |
# Parse the 'key' argument from the HTTP request |
| 39 |
key = self.get_query_argument("key", default=None) |
| 40 |
if key == 'au': |
| 41 |
# Return a string 'hit' if 'key' is equal to 'au' |
| 42 |
self.set_header("Content-Type", "text/plain") |
| 43 |
self.write("1.0") |
| 44 |
else: |
| 45 |
# Return a 404 HTTP error if 'key' is not equal to 'au' |
| 46 |
raise tornado.web.HTTPError(404) |
| 47 |
elif path == '/settings': |
| 48 |
self.set_header("Content-Type", "application/json") |
| 49 |
self.write("{\"actions\": { \"reject\": 1.0}, \"symbols\": { \"EXTERNAL_SETTINGS\": 1.0 }}") |
| 50 |
else: |
| 51 |
raise tornado.web.HTTPError(404) |
| 52 |
|
| 53 |
@tornado.gen.coroutine |
| 54 |
def post(self, path): |
| 55 |
if path == '/empty': |
| 56 |
# Return an empty reply |
| 57 |
self.set_header("Content-Type", "text/plain") |
| 58 |
self.write("") |
| 59 |
elif path == '/error_403': |
| 60 |
# Return a 403 HTTP error |
| 61 |
raise tornado.web.HTTPError(403) |
| 62 |
elif path == '/request': |
| 63 |
# Return a string 'hello post' |
| 64 |
self.set_header("Content-Type", "text/plain") |
| 65 |
self.write("hello post") |
| 66 |
elif path == '/timeout': |
| 67 |
# Wait for 4 seconds before returning an empty reply |
| 68 |
yield tornado.gen.sleep(4) |
| 69 |
self.set_header("Content-Type", "text/plain") |
| 70 |
self.write("") |
| 71 |
elif path == '/map-simple': |
| 72 |
# Return a string 'hello map' |
| 73 |
self.set_header("Content-Type", "text/plain") |
| 74 |
self.write("hello map") |
| 75 |
elif path == '/map-query': |
| 76 |
# Parse the 'key' argument from the HTTP request |
| 77 |
key = self.get_query_argument("key", default="") |
| 78 |
if key == 'au': |
| 79 |
# Return a string 'hit' if 'key' is equal to 'au' |
| 80 |
self.set_header("Content-Type", "text/plain") |
| 81 |
self.write("hit") |
| 82 |
else: |
| 83 |
# Return a 404 HTTP error if 'key' is not equal to 'au' |
| 84 |
raise tornado.web.HTTPError(404) |
| 85 |
elif path == '/settings': |
| 86 |
self.set_header("Content-Type", "application/json") |
| 87 |
self.write("{\"actions\": { \"reject\": 1.0}, \"symbols\": { \"EXTERNAL_SETTINGS\": 1.0 }}") |
| 88 |
else: |
| 89 |
raise tornado.web.HTTPError(404) |
| 90 |
|
| 91 |
def head(self, path): |
| 92 |
self.set_header("Content-Type", "text/plain") |
| 93 |
if path == "/redirect1": |
| 94 |
# Send an HTTP redirect to the bind address of the server |
| 95 |
self.redirect(f"{self.request.protocol}://{self.request.host}/hello") |
| 96 |
elif path == "/redirect2": |
| 97 |
# Send an HTTP redirect to the bind address of the server |
| 98 |
self.redirect(f"{self.request.protocol}://{self.request.host}/redirect1") |
| 99 |
elif self.path == "/redirect3": |
| 100 |
# Send an HTTP redirect to the bind address of the server |
| 101 |
self.redirect(f"{self.request.protocol}://{self.request.host}/redirect4") |
| 102 |
elif self.path == "/redirect4": |
| 103 |
# Send an HTTP redirect to the bind address of the server |
| 104 |
self.redirect(f"{self.request.protocol}://{self.request.host}/redirect3") |
| 105 |
else: |
| 106 |
self.send_response(200) |
| 107 |
self.set_header("Content-Type", "text/plain") |
| 108 |
|
| 109 |
def make_app(): |
| 110 |
return tornado.web.Application([ |
| 111 |
(r"(/[^/]+)", MainHandler), |
| 112 |
]) |
| 113 |
|
| 114 |
async def main(): |
| 115 |
parser = argparse.ArgumentParser() |
| 116 |
parser.add_argument("--bind", "-b", default="127.0.0.1", help="bind address") |
| 117 |
parser.add_argument("--port", "-p", type=int, default=18080, help="bind port") |
| 118 |
parser.add_argument("--keyfile", "-k", help="server private key file") |
| 119 |
parser.add_argument("--certfile", "-c", help="server certificate file") |
| 120 |
parser.add_argument("--pidfile", "-pf", help="path to the PID file") |
| 121 |
args = parser.parse_args() |
| 122 |
|
| 123 |
print(f"dummy_http.py: Starting server on {args.bind}:{args.port}", file=sys.stderr) |
| 124 |
|
| 125 |
# Create the Tornado application |
| 126 |
app = make_app() |
| 127 |
|
| 128 |
# If keyfile and certfile are provided, create an HTTPS server. |
| 129 |
# Otherwise, create an HTTP server. |
| 130 |
if args.keyfile and args.certfile: |
| 131 |
print(f"dummy_http.py: Using SSL with cert={args.certfile}, key={args.keyfile}", file=sys.stderr) |
| 132 |
ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) |
| 133 |
ssl_ctx.load_cert_chain(args.certfile, args.keyfile) |
| 134 |
server = tornado.httpserver.HTTPServer(app, ssl_options=ssl_ctx) |
| 135 |
else: |
| 136 |
server = tornado.httpserver.HTTPServer(app) |
| 137 |
|
| 138 |
# Start the server |
| 139 |
server.bind(args.port, args.bind) |
| 140 |
server.start(1) |
| 141 |
|
| 142 |
# Write the PID to the specified PID file, if provided |
| 143 |
# Do this AFTER successful bind/start so we know the server is actually running |
| 144 |
if args.pidfile: |
| 145 |
dummy_killer.write_pid(args.pidfile) |
| 146 |
print(f"dummy_http.py: PID file written to {args.pidfile}", file=sys.stderr) |
| 147 |
|
| 148 |
print(f"dummy_http.py: Server started successfully, listening on {args.bind}:{args.port}", file=sys.stderr) |
| 149 |
|
| 150 |
await asyncio.Event().wait() |
| 151 |
|
| 152 |
if __name__ == "__main__": |
| 153 |
try: |
| 154 |
if hasattr(asyncio, 'run') and callable(getattr(asyncio, 'run')): |
| 155 |
asyncio.run(main()) |
| 156 |
else: |
| 157 |
loop = asyncio.get_event_loop() |
| 158 |
loop.run_until_complete(main()) |
| 159 |
except Exception as e: |
| 160 |
print(f"dummy_http.py: FATAL ERROR: {type(e).__name__}: {e}", file=sys.stderr) |
| 161 |
traceback.print_exc(file=sys.stderr) |
| 162 |
sys.exit(1) |