Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. '''
  2.  
  3. Online Python Compiler.
  4. Code, Compile, Run and Debug python program online.
  5. Write your code in this editor and press "Run" button to execute it.
  6.  
  7. '''
  8.  
  9. import json
  10. import urllib.parse
  11. from http import server
  12.  
  13.  
  14. lines = []
  15. file = None
  16.  
  17. try:
  18. file = open('RU.txt', 'r', encoding="utf-8")
  19. lines = file.readlines()
  20. finally:
  21. file.close()
  22.  
  23. for line in lines:
  24. line = line.split('\t')
  25.  
  26.  
  27. class CustomHandler(server.SimpleHTTPRequestHandler):
  28.  
  29. def do_GET(self):
  30. conter = {"error": True}
  31. p = self.path.split('?')
  32.  
  33. if p[0] == '/geonameid':
  34. geonameid = p[1].split('=')
  35. id = geonameid[1]
  36.  
  37. res = [line for line in lines if id == line[0] and "P" == line[6]]
  38. content = {
  39. "error": False,
  40. "Information about city": res
  41.  
  42. }
  43.  
  44. elif p[0] == '/geolist':
  45. page_param = p[1].split('&')
  46. page_number = int(page_param[0].split('='))
  47. page_lenght = int(page_param[1].split('='))
  48.  
  49. cities = [line for line in lines if line[6] == "P"]
  50.  
  51. cities = cities[(page_n - 1) * lenght:page_n * lenght]
  52. content = {"error": False,
  53. "cities": cities}
  54.  
  55. elif p[0] == '/tip':
  56. tip_message = p[1].split('=')
  57. city_part_name = tip_message[1]
  58. city_part_name1 = urllib.parse.unquote(city_part_name)
  59.  
  60. tip = [line[3] for line in lines if city_part_name1.lower() in line[3].lower()]
  61. content = {
  62. "error": False,
  63. "tip": tip
  64. }
  65.  
  66. elif p[0] == '/geocompare':
  67.  
  68. compared_cities = p[1].split('&')
  69. first_city = compared_cities[0].split('=')
  70. second_city = compared_cities[1].split('=')
  71. first_city = urllib.parse.unquote(first_city[1])
  72. second_city = urllib.parse.unquote(second_city[1])
  73.  
  74. first_city_list = []
  75. second_city_list = []
  76.  
  77. count = -1
  78. for line in lines:
  79. line = line.split('\t')
  80. line_names = line[3].split(',')
  81. line_check = first_city
  82. if line_check in line_names and int(line[14]) > count:
  83. count = int(line[14])
  84. first_city_list = line
  85.  
  86.  
  87. count2 = -1
  88. for line in lines:
  89. line = line.split('\t')
  90. line_names = line[3].split(',')
  91. line_check = second_city
  92. if line_check in line_names and int(line[14]) > count2:
  93. count2 = int(line[14])
  94. second_city_list = line
  95.  
  96. print(first_city_list)
  97. print(second_city_list)
  98.  
  99. if first_city_list[4] > second_city_list[4]:
  100. north_city = first_city
  101. elif first_city_list[4] < second_city_list[4]:
  102. north_city = second_city
  103. else:
  104. north_city = 'Находятся на одинаковой широте'
  105.  
  106. print(north_city)
  107.  
  108. if first_city_list[17] == second_city_list[17]:
  109. time_zone = 'Одинаковая'
  110. else:
  111. time_zone = 'разная'
  112.  
  113. print(time_zone)
  114.  
  115. content = {"error": False,
  116. "First city": first_city_list,
  117. "Second city": second_city_list,
  118. "to the north is": north_city,
  119. "time zone": time_zone}
  120.  
  121. else:
  122. content["error_msg"] = "Bad request"
  123.  
  124. content = json.dumps(content)
  125. content = content.encode('utf-8')
  126.  
  127. self.protocol_version = 'HTTP/1.1'
  128. self.send_response(200)
  129. self.send_header('Content-Type', 'application/json; charset=utf-8')
  130.  
  131. self.end_headers()
  132. self.wfile.write(content)
  133.  
  134.  
  135.  
  136. server_address = ('127.0.0.1', 8000)
  137. http = server.HTTPServer(server_address, CustomHandler)
  138. http.serve_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement