main.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from http.server import HTTPServer, BaseHTTPRequestHandler
  2. class HttpGetHandler(BaseHTTPRequestHandler):
  3. def do_GET(self):
  4. try:
  5. if self.path.endswith("/OK"):
  6. self.send_response(200)
  7. self.send_header("Content-type", "text/html")
  8. self.end_headers()
  9. http_text = """ <html><head><meta charset='utf-8'>
  10. <title>Простой HTTP-сервер.</title></head>
  11. <body>Это главная страница.</body></html> """
  12. self.wfile.write(http_text.encode())
  13. if self.path.endswith("/info"):
  14. self.send_response(200)
  15. self.send_header("Content-type", "text/html")
  16. self.end_headers()
  17. http_text = """ <html><meta charset='utf-8'>
  18. <body>Кривицкий В.А. - группа 703</body></html> """
  19. self.wfile.write(http_text.encode())
  20. except IOError:
  21. self.send_error(400, f"File not found{self.path}")
  22. def main(server_class=HTTPServer, handler_class=HttpGetHandler):
  23. server_address = ('localhost', 8000)
  24. httpd = server_class(server_address, handler_class)
  25. try:
  26. print("Запускаем сервер!")
  27. httpd.serve_forever()
  28. except KeyboardInterrupt:
  29. httpd.server_close()
  30. print("Сервер остановлен!")
  31. if __name__ == '__main__':
  32. main()