__init__.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2025 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. test.cmdtest_d.httpd: WSGI http server
  12. """
  13. from wsgiref.simple_server import make_server, WSGIRequestHandler
  14. from mmgen.util import msg
  15. from mmgen.util2 import port_in_use
  16. class SilentRequestHandler(WSGIRequestHandler):
  17. def log_request(self, code='-', size='-'):
  18. return None
  19. class HTTPD:
  20. def start(self):
  21. if port_in_use(self.port):
  22. msg(f'\nPort {self.port} in use. Assuming {self.name} is running')
  23. return True
  24. self.httpd = make_server(
  25. 'localhost',
  26. self.port,
  27. self.application,
  28. handler_class = SilentRequestHandler)
  29. import threading
  30. t = threading.Thread(target=self.httpd.serve_forever, name=f'{self.name} thread')
  31. t.daemon = True
  32. t.start()
  33. def stop(self):
  34. self.httpd.server_close()
  35. def application(self, environ, start_response):
  36. method = environ['REQUEST_METHOD']
  37. response_body = self.make_response_body(method, environ)
  38. status = '200 OK'
  39. response_headers = [
  40. ('Content-Type', self.content_type),
  41. ('Content-Length', str(len(response_body)))
  42. ]
  43. start_response(status, response_headers)
  44. return [response_body]