web1.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from http.server import HTTPServer, BaseHTTPRequestHandler
  2. from datetime import datetime
  3. import re
  4. class HttpGetHandler(BaseHTTPRequestHandler):
  5. def do_GET(self):
  6. try:
  7. if self.path.endswith("/"):
  8. self.send_response(200)
  9. self.send_header("Content-type", "text/html")
  10. self.end_headers()
  11. http_text = """<html><head><meta charset='utf-8'>
  12. <title>Main</title></head>
  13. <body>Главная страница<br>
  14. <a href="http://localhost:8000/Info">Info</a></body><html><br>
  15. <a href="http://localhost:8000/Status">Status</a></body><html>"""
  16. self.wfile.write(http_text.encode())
  17. if self.path.endswith("/Info"):
  18. self.send_response(200)
  19. self.send_header("Content-type", "text/html")
  20. self.end_headers()
  21. http_text = """<html><head><meta charset='utf-8'>
  22. <title>Info student</title></head>
  23. <body>Чичкова Виорика Владимировна группа 701(3)<br>
  24. <a href="http://localhost:8000/">На главную</a></body><html>"""
  25. self.wfile.write(http_text.encode())
  26. if self.path.endswith("/Status"):
  27. self.send_response(200)
  28. add=self.client_address[0]
  29. add=re.sub('[.]([0-9]{1,3})', ".x", add, 2)
  30. time=datetime.now().strftime("%Y-%m-%d %I:%M:%S%p")
  31. self.send_header("Content-type", "text/html")
  32. self.end_headers()
  33. http_text = f"<html><head><meta charset='utf-8'>" \
  34. f"<title>Status</title></head>" \
  35. f"<body>Ваш IP: {add}<br>" \
  36. f"ФИО: Чичкова Виорика Владимировна <br>" \
  37. f"Дата: {time}<br><br>" \
  38. f"<a href='http://localhost:8000/'>На главную</body></html>"
  39. self.wfile.write(http_text.encode())
  40. except IOError:
  41. self.send_error(400,f"File not found{self.path}")
  42. def main(server_class=HTTPServer,handler_class=HttpGetHandler):
  43. server_address = ('localhost',8000)
  44. httpd = server_class(server_address,handler_class)
  45. try:
  46. print("Start!")
  47. httpd.serve_forever()
  48. except KeyboardInterrupt:
  49. httpd.server_close()
  50. print("Stop!")
  51. if __name__ == '__main__':
  52. main()