server.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. from http.server import BaseHTTPRequestHandler, HTTPServer
  2. import json
  3. import random
  4. import smtplib
  5. from email.mime.text import MIMEText
  6. HOST_NAME = 'localhost'
  7. PORT_NUMBER = 8000
  8. # Глобальный объект сервера
  9. server = None
  10. class MyHandler(BaseHTTPRequestHandler):
  11. def do_POST(self):
  12. if self.path == '/send_code':
  13. self.handle_send_code()
  14. elif self.path == '/register':
  15. self.handle_registration()
  16. else:
  17. self.send_error(404, 'Not Found')
  18. def handle_send_code(self):
  19. content_length = int(self.headers['Content-Length'])
  20. body = self.rfile.read(content_length).decode('utf-8')
  21. data = json.loads(body)
  22. email = data['email']
  23. # Проверка на Gmail
  24. if not email.endswith('@gmail.com'):
  25. self.send_response(400)
  26. self.send_header('Content-type', 'text/plain')
  27. self.send_header('Access-Control-Allow-Origin', '*')
  28. self.send_header('Access-Control-Allow-Methods', 'POST, OPTIONS')
  29. self.send_header('Access-Control-Allow-Headers', 'Content-Type')
  30. self.end_headers()
  31. self.wfile.write(bytes('Пожалуйста, введите адрес электронной почты Gmail.', 'utf-8'))
  32. return
  33. code = str(random.randint(100000, 999999))
  34. try:
  35. msg = MIMEText(f'Ваш код подтверждения: {code}')
  36. msg['Subject'] = 'Код подтверждения'
  37. msg['From'] = 'damirka20051904@gmail.com' # Замените на ваш адрес
  38. msg['To'] = email
  39. with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
  40. server.login('damirka20051904@gmail.com', 'DaMiR_2005') # Замените на ваш логин и пароль
  41. server.sendmail('damirka20051904@gmail.com', email, msg.as_string())
  42. self.send_response(200)
  43. self.send_header('Content-type', 'text/plain')
  44. self.send_header('Access-Control-Allow-Origin', '*')
  45. self.send_header('Access-Control-Allow-Methods', 'POST, OPTIONS')
  46. self.send_header('Access-Control-Allow-Headers', 'Content-Type')
  47. self.end_headers()
  48. self.wfile.write(bytes('Код успешно отправлен', 'utf-8'))
  49. except Exception as e:
  50. self.send_response(500)
  51. self.send_header('Content-type', 'text/plain')
  52. self.send_header('Access-Control-Allow-Origin', '*')
  53. self.send_header('Access-Control-Allow-Methods', 'POST, OPTIONS')
  54. self.send_header('Access-Control-Allow-Headers', 'Content-Type')
  55. self.end_headers()
  56. self.wfile.write(bytes(f'Ошибка отправки кода: {e}', 'utf-8'))
  57. def handle_registration(self):
  58. content_length = int(self.headers['Content-Length'])
  59. body = self.rfile.read(content_length).decode('utf-8')
  60. data = json.loads(body)
  61. name = data['name']
  62. phone = data['phone']
  63. email = data['email']
  64. code = data['code']
  65. password = data['password']
  66. # Здесь должна быть логика для проверки кода,
  67. # записи данных в базу данных и т.д.
  68. # В этом примере просто отправляем сообщение об успешной регистрации
  69. self.send_response(200)
  70. self.send_header('Content-type', 'text/plain')
  71. self.send_header('Access-Control-Allow-Origin', '*')
  72. self.send_header('Access-Control-Allow-Methods', 'POST, OPTIONS')
  73. self.send_header('Access-Control-Allow-Headers', 'Content-Type')
  74. self.end_headers()
  75. self.wfile.write(bytes('Регистрация успешна', 'utf-8'))
  76. def do_OPTIONS(self):
  77. self.send_response(200)
  78. self.send_header('Access-Control-Allow-Origin', '*')
  79. self.send_header('Access-Control-Allow-Methods', 'POST, OPTIONS')
  80. self.send_header('Access-Control-Allow-Headers', 'Content-Type')
  81. self.end_headers()
  82. class MyHandler(BaseHTTPRequestHandler):
  83. def do_GET(self):
  84. if self.path == 'документы/Устав.docx':
  85. self.handle_file_download('Устав.docx')
  86. else:
  87. self.send_error(404, 'Not Found')
  88. def handle_file_download(self, filename):
  89. try:
  90. with open(filename, 'rb') as f:
  91. self.send_response(200)
  92. self.send_header('Content-type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')
  93. self.send_header('Content-Disposition', f'attachment; filename="{filename}"')
  94. self.end_headers()
  95. self.wfile.write(f.read())
  96. except FileNotFoundError:
  97. self.send_error(404, 'File not found')
  98. class MyHandler(BaseHTTPRequestHandler):
  99. def do_GET(self):
  100. if self.path == 'документы/Инструкция выгрузка подписи.docx':
  101. self.handle_file_download('Инструкция выгрузка подписи.docx')
  102. else:
  103. self.send_error(404, 'Not Found')
  104. def handle_file_download(self, filename):
  105. try:
  106. with open(filename, 'rb') as f:
  107. self.send_response(200)
  108. self.send_header('Content-type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')
  109. self.send_header('Content-Disposition', f'attachment; filename="{filename}"')
  110. self.end_headers()
  111. self.wfile.write(f.read())
  112. except FileNotFoundError:
  113. self.send_error(404, 'File not found')
  114. class MyHandler(BaseHTTPRequestHandler):
  115. def do_GET(self):
  116. if self.path == 'документы/РП_Данные_ГИС_ЖКХ_2_1_v1.docx':
  117. self.handle_file_download('РП_Данные_ГИС_ЖКХ_2_1_v1.docx')
  118. else:
  119. self.send_error(404, 'Not Found')
  120. def handle_file_download(self, filename):
  121. try:
  122. with open(filename, 'rb') as f:
  123. self.send_response(200)
  124. self.send_header('Content-type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')
  125. self.send_header('Content-Disposition', f'attachment; filename="{filename}"')
  126. self.end_headers()
  127. self.wfile.write(f.read())
  128. except FileNotFoundError:
  129. self.send_error(404, 'File not found')
  130. class MyHandler(BaseHTTPRequestHandler):
  131. def do_GET(self):
  132. if self.path == 'документы/РП_Оперативные_оповещения_2_1_v1.docx':
  133. self.handle_file_download('РП_Оперативные_оповещения_2_1_v1.docx')
  134. else:
  135. self.send_error(404, 'Not Found')
  136. def handle_file_download(self, filename):
  137. try:
  138. with open(filename, 'rb') as f:
  139. self.send_response(200)
  140. self.send_header('Content-type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')
  141. self.send_header('Content-Disposition', f'attachment; filename="{filename}"')
  142. self.end_headers()
  143. self.wfile.write(f.read())
  144. except FileNotFoundError:
  145. self.send_error(404, 'File not found')
  146. class MyHandler(BaseHTTPRequestHandler):
  147. def do_GET(self):
  148. if self.path == 'документы/регион_РП_Сообщения_жителей_2_1_v1.docx':
  149. self.handle_file_download('регион_РП_Сообщения_жителей_2_1_v1.docx')
  150. else:
  151. self.send_error(404, 'Not Found')
  152. def handle_file_download(self, filename):
  153. try:
  154. with open(filename, 'rb') as f:
  155. self.send_response(200)
  156. self.send_header('Content-type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')
  157. self.send_header('Content-Disposition', f'attachment; filename="{filename}"')
  158. self.end_headers()
  159. self.wfile.write(f.read())
  160. except FileNotFoundError:
  161. self.send_error(404, 'File not found')
  162. if __name__ == '__main__':
  163. # Создание глобального объекта сервера
  164. server = HTTPServer((HOST_NAME, PORT_NUMBER), MyHandler)
  165. print(f'Сервер запущен на http://{HOST_NAME}:{PORT_NUMBER}')
  166. try:
  167. server.serve_forever()
  168. except KeyboardInterrupt:
  169. server.server_close()
  170. print('Сервер остановлен')