| 57 | |
| 58 | |
| 59 | class InternalServer(HTTPServer): |
| 60 | |
| 61 | def kill_me(self): |
| 62 | self.shutdown() |
| 63 | logging.info(f"The server's life has come to an end, pid: {os.getpid()}") |
| 64 | |
| 65 | |
| 66 | def reset_selfdestruct_timer(self): |
| 67 | if self.self_destruct_timer: |
| 68 | self.self_destruct_timer.cancel() |
| 69 | |
| 70 | self.self_destruct_timer = Timer(LIFESPAN, self.kill_me) |
| 71 | self.self_destruct_timer.start() |
| 72 | |
| 73 | |
| 74 | def __init__(self, server_address, RequestHandlerClass, |
| 75 | bind_and_activate=True): |
| 76 | |
| 77 | HTTPServer.__init__(self, server_address, RequestHandlerClass, |
| 78 | bind_and_activate=bind_and_activate) |
| 79 | |
| 80 | self.self_destruct_timer = None |
| 81 | self.clients = 1 |
| 82 | self.reset_selfdestruct_timer() |
| 83 | |
| 84 | |
| 85 | def suicide(self): |
| 86 | self.clients -= 1 |
| 87 | if self.clients == 0: |
| 88 | if self.self_destruct_timer is not None: |
| 89 | self.self_destruct_timer.cancel() |
| 90 | |
| 91 | quick_and_painless_timer = Timer(0.1, self.kill_me) |
| 92 | quick_and_painless_timer.start() |
| 93 | |
| 94 | |
| 95 | class TestServer: |