main.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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>Простой HTTP-сервер.</title></head>
  11. <body>Код 200.</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><head><meta charset = 'utf-8'>
  18. <title>Простой HTTP-сервер.</title></head>
  19. <body>Анкудинов Арсений Андреевич,студент 701(3) группы.</body></html> """
  20. self.wfile.write(http_text.encode())
  21. except IOError:
  22. self.send_error(400,f"File not found{self.path}")
  23. def main(server_class=HTTPServer, handler_class=HttpGetHandler):
  24. server_address = ('localhost', 8000)
  25. httpd = server_class(server_address, handler_class)
  26. try:
  27. print("есть связь")
  28. httpd.serve_forever()
  29. except KeyboardInterrupt:
  30. httpd.serber_close()
  31. print("нет связи")
  32. if __name__ == '__main__':
  33. main()