laba1.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from http.server import HTTPServer
  2. from http.server import BaseHTTPRequestHandler
  3. class HttpGetHandler(BaseHTTPRequestHandler):
  4. def do_GET(self):
  5. try:
  6. if self.path.endswith("/Ok"):
  7. self.send_response(200)
  8. self.send_header("Content-type", "text/html")
  9. self.end_headers()
  10. http_text = """ <html><head><meta charset= 'utf-8'>
  11. <title>Simple HTTP-server.</title></head>
  12. <body>This is the main page.</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>Simple Info.</title></head>
  20. <body>Timofeeva Darya Dmitrievna gr 701(3).</body></html> """
  21. self.wfile.write(http_text.encode())
  22. except IOError:
  23. self.send_error(400, f"File not found{self.path}")
  24. def main(server_class=HTTPServer, handler_class=HttpGetHandler):
  25. server_address = ('localhost', 8000)
  26. httpd = server_class(server_address, handler_class)
  27. try:
  28. print("Starting the server!")
  29. httpd.serve_forever()
  30. except KeyboardInterrupt:
  31. httpd.server_close()
  32. print("The server is stopped!")
  33. if __name__ == '__main__' :
  34. main()