7
from urllib.parse import urlparse,parse_qs
10
file = os.path.join(os.path.dirname(__file__), path)
11
if os.path.isfile(file):
12
with open(file, 'r') as fp:
19
class MyRequestHandler(http.server.BaseHTTPRequestHandler):
21
sys.stderr.write("GET: %s\n" % self.path)
24
parse = urlparse(self.path)
26
query = parse_qs(parse.query)
28
if path == '/data/2.5/weather':
29
self.send_response(200)
30
self.send_header("Content-type", "text/html")
35
mode = query['mode'][0]
37
self.wfile.write(bytes(read_file('weather/%s.%s' % (query['q'][0], mode)), 'UTF-8'))
38
elif path == '/data/2.5/forecast/daily':
39
self.send_response(200)
40
self.send_header("Content-type", "text/html")
45
mode = query['mode'][0]
47
self.wfile.write(bytes(read_file('forecast/daily/%s.%s' % (query['q'][0], mode)), 'UTF-8'))
49
self.send_response(404)
50
self.send_header("Content-type", "text/html")
52
self.wfile.write(bytes('ERROR', 'UTF-8'))
54
if __name__ == "__main__":
55
Handler = MyRequestHandler
56
httpd = socketserver.TCPServer(("127.0.0.1", 0), Handler)
58
sys.stdout.write('%d\n' % httpd.server_address[1])