A small HTTP POST server
A couple of weeks ago Neil mentioned on Mastodon being interested in a privacy focused GPS logger for use in a car, which was odd, as I was thinking about the exact same thing a few days before. RevK jumped in to provide something, which Neil wrote about a short while later. I was lucky enough to be able to buy one of the first versions (everyone can get one now), but was a bit stuck for time to play with it initially, which I’ve been able to remedy over the last couple of days.
It’s a fabulous device, with lots of lovely touches - loads of LEDs used to show which GPS satellites are in use, upload progress, OTA upgrade progress, etc. Really great stuff.
One of the nice features of the firmware is that it will upload traces to an HTTP server of your choice with a POST request. While most HTTP servers support this, it can be a bit of a faff getting things all plumbed together when you just want to test something. To that end, here is a small HTTP POST server in Python that can be used for testing:
#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, HTTPServer
class handler(BaseHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
self.send_response(200)
self.end_headers()
file = self.path[2:]
with open(file, 'wb') as out:
out.write(body)
print(f"Wrote {content_length} bytes to {file}.")
with HTTPServer(('', 8000), handler) as server:
server.serve_forever()
This listens on port 8000 on the local machine without SSL. Configure your logger to point at it, and it should upload the relevant files (GPX in my case), which are then stored in the current directory:
172.16.100.162 - - [02/Feb/2024 10:24:38] "POST /?logger1-2024-02-01T21-46-06Z.gpx HTTP/1.1" 200 -
Wrote 131661 bytes to logger1-2024-02-01T21-46-06Z.gpx.
172.16.100.162 - - [02/Feb/2024 10:24:52] "POST /?logger1-2024-02-01T22-00-01Z.gpx HTTP/1.1" 200 -
Wrote 30774 bytes to logger1-2024-02-01T22-00-01Z.gpx.
This is not likely to be something that you’d use long term, but it’s convenient for testing.
Next up is to have the server provide access to the uploaded GPX traces using Leaflet maps for visualisation, but that’s for next week.