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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python3
  2. import http.server
  3. import os
  4. import socket
  5. import socketserver
  6. import sys
  7. import time
  8. import dummy_killer
  9. PORT = 18080
  10. HOST_NAME = '0.0.0.0'
  11. PID = "/tmp/dummy_http.pid"
  12. class MyHandler(http.server.BaseHTTPRequestHandler):
  13. def setup(self):
  14. http.server.BaseHTTPRequestHandler.setup(self)
  15. self.protocol_version = "HTTP/1.1" # allow connection: keep-alive
  16. def do_HEAD(self):
  17. if self.path == "/redirect1":
  18. self.send_response(301)
  19. self.send_header("Location", "http://127.0.0.1:"+str(PORT)+"/hello")
  20. elif self.path == "/redirect2":
  21. self.send_response(301)
  22. self.send_header("Location", "http://127.0.0.1:"+str(PORT)+"/redirect1")
  23. elif self.path == "/redirect3":
  24. self.send_response(301)
  25. self.send_header("Location", "http://127.0.0.1:"+str(PORT)+"/redirect4")
  26. elif self.path == "/redirect4":
  27. self.send_response(301)
  28. self.send_header("Location", "http://127.0.0.1:"+str(PORT)+"/redirect3")
  29. else:
  30. self.send_response(200)
  31. self.send_header("Content-type", "text/html")
  32. self.end_headers()
  33. self.log_message("to be closed: " + repr(self.close_connection))
  34. def do_GET(self):
  35. response = b"hello world"
  36. """Respond to a GET request."""
  37. if self.path == "/empty":
  38. self.finish()
  39. return
  40. if self.path == "/timeout":
  41. time.sleep(2)
  42. if self.path == "/error_403":
  43. self.send_response(403)
  44. else:
  45. self.send_response(200)
  46. if self.path == "/content-length":
  47. self.send_header("Content-Length", str(len(response)))
  48. self.send_header("Content-type", "text/plain")
  49. self.end_headers()
  50. self.wfile.write(response)
  51. self.log_message("to be closed: %d, headers: %s, conn:'%s'" % (self.close_connection, str(self.headers), self.headers.get('Connection', "").lower()))
  52. conntype = self.headers.get('Connection', "").lower()
  53. if conntype != 'keep-alive':
  54. self.close_connection = True
  55. self.log_message("ka:'%s', pv:%s[%s]" % (str(conntype == 'keep-alive'), str(self.protocol_version >= "HTTP/1.1"), self.protocol_version))
  56. def do_POST(self):
  57. response = b"hello post"
  58. """Respond to a GET request."""
  59. if self.path == "/empty":
  60. self.finish()
  61. return
  62. if self.path == "/timeout":
  63. time.sleep(2)
  64. if self.path == "/error_403":
  65. self.send_response(403)
  66. else:
  67. self.send_response(200)
  68. if self.path == "/content-length":
  69. self.send_header("Content-Length", str(len(response)))
  70. self.send_header("Content-type", "text/plain")
  71. self.end_headers()
  72. self.wfile.write(response)
  73. class ThreadingSimpleServer(socketserver.ThreadingMixIn,
  74. http.server.HTTPServer):
  75. def __init__(self):
  76. self.allow_reuse_address = True
  77. self.timeout = 1
  78. http.server.HTTPServer.__init__(self, (HOST_NAME, PORT), MyHandler)
  79. def run(self):
  80. dummy_killer.write_pid(PID)
  81. try:
  82. while 1:
  83. sys.stdout.flush()
  84. server.handle_request()
  85. except KeyboardInterrupt:
  86. print("Interrupt")
  87. except socket.error:
  88. print("Socket closed")
  89. def stop(self):
  90. self.keep_running = False
  91. self.server_close()
  92. if __name__ == '__main__':
  93. server = ThreadingSimpleServer()
  94. dummy_killer.setup_killer(server, server.stop)
  95. server.run()