Skip to content

Instantly share code, notes, and snippets.

@rskupnik
Created January 18, 2019 19:43
Show Gist options
  • Save rskupnik/0b7929d7548b0fa201c427fd35778717 to your computer and use it in GitHub Desktop.
Save rskupnik/0b7929d7548b0fa201c427fd35778717 to your computer and use it in GitHub Desktop.
HTTP server that allows for adding and authenticating a fingerprint with a Waveshare fingerprint scanner
#!/usr/bin/env python
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer
import serial
ser = serial.Serial("/dev/serial0", baudrate=19200, timeout = 5)
def send_bytes(bytes):
tosend = [0xF5] + bytes + [bytes[0]^bytes[4], 0xF5]
ser.write(tosend[::-1])
def receive_bytes():
bytes = ser.read(8)
barr = bytearray(bytes)
return barr
def verify_success(bytes):
return len(bytes) == 8 and bytes[4] == 0x00
def add_fingerprint():
send_bytes([ 0x01, 0x00, 0x01, 0x01, 0x00 ])
bytes = receive_bytes()
if not verify_success(bytes):
return False
send_bytes([ 0x02, 0x00, 0x01, 0x01, 0x00 ])
bytes = receive_bytes()
if not verify_success(bytes):
return False
send_bytes([ 0x03, 0x00, 0x01, 0x01, 0x00 ])
bytes = receive_bytes()
if not verify_success(bytes):
return False
return True
def auth_fingerprint():
send_bytes([ 0x0C, 0x00, 0x00, 0x00, 0x00 ])
bytes = receive_bytes()
return len(bytes) == 8 and bytes[4] == 0x01
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
result = auth_fingerprint()
if result:
self.send_response(200)
else:
self.send_response(403)
self.send_header('Content-type', 'text/plain')
self.end_headers()
def do_HEAD(self):
self._set_headers()
def do_POST(self):
self._set_headers()
result = add_fingerprint()
self.wfile.write("<html><body><h1>Fingerprint added: "+str(result)+"</h1></body></html>")
def run(server_class=HTTPServer, handler_class=S, port=80):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print 'Server started'
httpd.serve_forever()
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment