You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dummy_http.py 5.7KB

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