Created
August 31, 2021 06:41
-
-
Save mdav43/960fddcae61288e10c0abd6d69f65915 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import curdir | |
from os.path import join as pjoin | |
import sys, datetime, time, glob, os | |
from systemd import journal | |
from http.server import BaseHTTPRequestHandler, HTTPServer | |
class StoreHandler(BaseHTTPRequestHandler): | |
store_path = pjoin(curdir, 'store') | |
# in overland the url will be http://yourservername_or_ip:8383/store | |
def do_GET(self): | |
if self.path == '/store': | |
list_of_files = glob.glob('*.geojson') # * means all if need specific format then *.csv | |
latest_file = max(list_of_files, key=os.path.getctime) | |
with open(latest_file) as fh: | |
self.send_response(200) | |
self.send_header('Content-type', 'text/json') | |
self.end_headers() | |
self.wfile.write(fh.read().encode()) | |
def do_POST(self): | |
if self.path == '/store': | |
length = self.headers['content-length'] | |
data = self.rfile.read(int(length)) | |
current_date_and_time_string = time.strftime("%Y%m%d-%H%M%S") | |
extension = ".geojson" | |
file_name = current_date_and_time_string + extension | |
journal.write("Attempting: Recording data from iOS") | |
with open(file_name, 'w') as fh: | |
fh.write(data.decode()) | |
journal.write("Completed: Recording data from iOS") | |
self.send_response(200) | |
self.send_header('Content-Type', 'application/json') | |
self.end_headers() | |
output = b"" | |
output += b'{"result": "ok"}' | |
self.wfile.write(output.encode()) | |
journal.write("Responded: Overland") | |
server = HTTPServer(('', 8383), StoreHandler) | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment