-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop_server.py
executable file
·29 lines (25 loc) · 1.03 KB
/
loop_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/python3
'''
This hosts a WSGI server on localhost:8000 that returns a JSON file containing the data from the most recent LOOP packet.
'''
from wsgiref.simple_server import make_server
import sys
# This is the file location of the JSON file:
filename = "/home/weather/loop_packet.json"
# If a file has been specified in a command line argument, use that:
if len(sys.argv) > 1: filename = sys.argv[1]
# This is the WSGI handler:
def serve_json(environ, start_response):
# Get the data from the JSON file:
with open(filename, "rb") as file: data = file.read()
# Start response with 200 status and JSON content-type:
# Remember to add Access-Control-Allow-Origin so AJAX will work.
start_response("200 OK", [
("Content-Type", "application/json; charset=utf-8"), ("Content-Length", str(len(data))),
("Access-Control-Allow-Origin", "*")
])
# Return the data from the file:
return [data]
# Host the WSGI server on localhost:8000
httpd = make_server("localhost", 8000, serve_json)
httpd.serve_forever()