python.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from http.server import HTTPServer, BaseHTTPRequestHandler
  2. import requests
  3. import re
  4. from datetime import datetime
  5. class HttpGetHandler(BaseHTTPRequestHandler):
  6. def do_GET(self):
  7. try:
  8. if self.path.endswith("/"):
  9. self.send_response(200)
  10. self.send_header("Content-type", "text/html")
  11. self.end_headers()
  12. http_text = """<html><head><meta charset='utf-8'>
  13. <title>Simple HTTP Server</title></head>
  14. <body>Ok<br>
  15. <a href="http://localhost:8000/info">Info</a></body><html>
  16. <a href="http://localhost:8000/Status">Status</a></body><html>"""
  17. self.wfile.write(http_text.encode())
  18. if self.path.endswith("/info"):
  19. self.send_response(200)
  20. self.send_header("Content-type", "text/html")
  21. self.end_headers()
  22. http_text = """<html><head><meta charset='utf-8'>
  23. <title>Info</title></head>
  24. <body>Info<br>
  25. <body>Kapitanyuk Alexey Konstantinovich<br>
  26. <body>gr-701(3)<br>
  27. <a href="http://localhost:8000/">Ok</a></body><html>
  28. <a href="http://localhost:8000/Status">Status</a></body><html>"""
  29. self.wfile.write(http_text.encode())
  30. if self.path.endswith("/Status"):
  31. self.send_response(200)
  32. self.send_header("Content-type", "text/html")
  33. self.end_headers()
  34. pattern = r'\d{2,}\.\d{2,}\.\d{2,}\.\d{2,}'
  35. ip = re.search(pattern, html).group()
  36. dt = datetime.now().strftime("%Y-%m-%d %I:%M:%S %p")
  37. http_text = f"Status" \
  38. f"FIO: Kapitanyuk Alexey Konstantinovich<br>" \
  39. f"Date and time: {dt}<br><br>" \
  40. f"Ip-address: {ip} <br>" \
  41. f"<a href='http://localhost:8000/'>Ok</a><br>" \
  42. f"<a href='http://localhost:8000/info'>Info</a></body></html>"
  43. self.wfile.write(http_text.encode())
  44. except IOError:
  45. self.send_error(400,f"File not found{self.path}")
  46. def main(server_class=HTTPServer,handler_class=HttpGetHandler):
  47. server_address = ('localhost',8000)
  48. httpd = server_class(server_address,handler_class)
  49. try:
  50. print("Start server")
  51. httpd.serve_forever()
  52. except KeyboardInterrupt:
  53. httpd.server_close()
  54. print("Kill server")
  55. if __name__ == '__main__':
  56. main()