#! /usr/bin/python import sys import psycopg2 import datetime import time import random station = 1 def Login(c): id = int(raw_input("Give me your ID: ")) c.execute("""SELECT time, strength FROM sputnik.reader WHERE station = %s AND id = %s AND time > now()-'5 second'::interval""", (station, id,)) found = False strength = 255 i = c.fetchone() while i != None: found = True if strength > i[1]: strength = i[1] i = c.fetchone() if not found: print "You are not visible at the computer" return False if strength > 0x55: print "You are to far away from the computer" return False b = random.randrange(1, 6) print "Press button exactly %i times." % (b, ) time.sleep(5) c.execute("""SELECT time, strength, tags FROM sputnik.reader WHERE station = %s AND id = %s AND time > now()-'5 second'::interval""", (station, id,)) found = False strength = 255 pressed = False i = c.fetchone() while i != None: found = True if strength > i[1]: strength = i[1] if i[2] != None and len(i[2]) > 1: if i[2][0] == 'button0' and int(i[2][1]) == b: pressed = True i = c.fetchone() if not found: print "You are not visible at the computer" return False if strength > 0x55: print "You are to far away from the computer" return False if not pressed: print "You have not pressed button exactly %i times." % (b, ) return False return True connection = psycopg2.connect("dbname=tomus") c = connection.cursor() if Login(c): print "Welcome, master." else: print "Go away, you impostor!" connection.commit() c.close() c = None connection.close() connection = None