Code.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from http.server import HTTPServer, BaseHTTPRequestHandler
  2. class HttpGetHandler(BaseHTTPRequestHandler):
  3. def do_GET(self):
  4. try:
  5. if self.path.endswith("/"):
  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>Simple HTTP Server</title></head>
  11. <body>Типа главная страница<br>
  12. <a href="http://localhost:8000/info">Info</a></body><html>"""
  13. self.wfile.write(http_text.encode())
  14. if self.path.endswith("/info"):
  15. self.send_response(200)
  16. self.send_header("Content-type", "text/html")
  17. self.end_headers()
  18. http_text = """<html><head><meta charset='utf-8'>
  19. <title>Емае</title></head>
  20. <body>Кипиченков Никита Вячеславович 701(3)<br>
  21. <a href="http://localhost:8000/">На главную</a></body><html>"""
  22. self.wfile.write(http_text.encode())
  23. except IOError:
  24. self.send_error(400,f"File not found{self.path}")
  25. def main(server_class=HTTPServer,handler_class=HttpGetHandler):
  26. server_address = ('localhost',8000)
  27. httpd = server_class(server_address,handler_class)
  28. try:
  29. print("Starting the Server!")
  30. httpd.serve_forever()
  31. except KeyboardInterrupt:
  32. httpd.server_close()
  33. print("Killing the Server!")
  34. if __name__ == '__main__':
  35. main()