Skip to content

Instantly share code, notes, and snippets.

@mdav43
Created August 31, 2021 06:41
Show Gist options
  • Save mdav43/960fddcae61288e10c0abd6d69f65915 to your computer and use it in GitHub Desktop.
Save mdav43/960fddcae61288e10c0abd6d69f65915 to your computer and use it in GitHub Desktop.
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